Multiple Converters/Generators in Jekyll? - ruby

I would like to write a Jekyll plugin that makes all posts available in PDF format by utilizing Kramdown's LaTeX export capabilities. For each post in Markdown format, I'd like to end up with the normal .html post along with a .tex file containing the LaTeX markup and finally a .pdf.
Following the documentation for creating plugins, I see two ways of approaching the problem, either with a Converter or with a Generator.
Converter plugins seem to run after the built-in Converters, so the .markdown files have all been converted to .html by the time they reach the Converter.
When I try to implement a Generator, I am able to use fileutils to write a file successfully, but by the end of Jekyll's cycle, that file has been removed. It seems there's a StaticFile class which you can use to register new output files with Jekyll, but I cannot find any real guidance on how to use it.

If you take a look at the ThumbGenerator class in this: https://github.com/matthewowen/jekyll-slideshow/blob/master/_plugins/jekyll_slideshow.rb you'll seen a similar example. This particular plugin makes thumbnail sized versions of all images in the site. Hopefully it gives a useful guide to how you can interact with Jekyll's StaticFile class (though I'm not a Ruby pro, so forgive any poor style).
Unfortunately, there isn't really documentation for this - I gleaned it from reading through the source.
I wrote this a few months ago and don't particularly remember the details (which is why I gave an example rather than a workthrough), but if this doesn't get you on the right track let me know and I'll try to help.

I try to do the same but with direct html->pdf conversion.
It did not work inside a gitlab-ci pipeline at this time, nonetheless it work on my workstation (see here) with a third possibility : a hook !
(here with pdfkit)
require 'pdfkit'
module Jekyll
Jekyll::Hooks.register :site, :post_write do |post|
post.posts.docs.each do |post|
filename = post.site.dest + post.id + ".pdf"
dirname = File.dirname(filename)
Dir.mkdir(dirname) unless File.exists?(dirname)
kit = PDFKit.new(post.content, :page_size => 'Letter')
kit.stylesheets << './css/bootstrap.min.css'
kit.to_file(filename)
end
end
end

Related

How to translate comments in code blocks using Sphinx

I write documentation for a project using Sphinx. I have code examples (defined as code-blocks, both literally included and just typed inline). In these code examples there are comments, but when I produce documentation translation, they are not extracted into .po files, and obviously are not translated.
How can I translate comments in code examples?
I found other questions about Sphinx modifications. The answers proposed modifying conf.py (making some hooks), creating roles or extensions. I never did that before and I don't know where to start and what solution would be better. Is there any existing solution for this problem?
UPD. These are examples of code I want to show in my documentation:
git clone https://github.com/ynikitenko/lena
# most of requirements are for development only
pip install -r lena/requirements.txt
(here I'd like to translate the comment). A more difficult (maybe not so needed) example is this:
class End(object):
"""Stop sequence here."""
def run(self, flow):
"""Exhaust all preceding flow and stop iteration
(yield nothing to the following flow).
"""
for val in flow:
pass
return
# otherwise it won't be a generator
yield "unreachable"
These examples are formatted with the directive
.. code-block::
I wrote to the official sphinx-users google group, and this is the answer from Matt from Documatt:
It's impossible. Sphinx will have to understand comments in every language.
If you want to translate comments in code-block (and literal blocks after ::), you must translate them all. Add gettext_additional_targets = ["literal-block"] to your conf.py and re-run POT/PO update.
The code lines remain in the "translations" of code, but now the problem is solved for me.

How can I add an extension that uses the internal Gollum markdown processor?

Description:
I am trying to integrate a workflow and tools which incorporates vimwiki and Gollum. I want to merely add vimwiki as an extention type for the editor while being processed by Gollum's internal Markdown processor (see "Things I've tried" #1).
Eventually I'd also like to have Gollum default to 'vimwiki' when creating new documents as well.
The project I'm working on can be found at Vimwiki-Gollum-Integration
Testing Specifics:
Gollum version: 4.1.1
The file being tested is valid github markdown
The same content is being tested with different filenames and extensions:
testx.thing
blah.vimwiki
The test files do work fine when they are named with .md extensions
relevant rendering gems installed:
github-markdown
github-markup
kramdown
redcarpet
All code is being tested in a gollum --config file
I'm not well versed in ruby
Things I've tried
Creating a new extension and custom renderer
This works except there is no reason to implement a custom processor (pandoc)
This would be ideal so that the Markdown name ends up in the Edit page. However, I don't think I should have to involve pandoc here
How in the heck do I get the edit page to default the markdown to vimwiki when editing a vimwiki extension?
file used: blah.vimwiki
# always include this:
Gollum::Page.send :remove_const, :FORMAT_NAMES if defined? Gollum::Page::FORMAT_NAMES
################### custom extension + renderer
# # Custom extension rendering
# ## References
# * file reference: /var/lib/gems/2.1.0/gems/github-markup-1.6.0/lib/github/markup/command_implementation.rb
# * [Adding Pandoc to Gollum - Martin Wolf's weblog [OUTDATED]](https://www.mwolf.net/2014/04/29/adding-pandoc-to-gollum/)
ci = ::GitHub::Markup::CommandImplementation.new(
/vimwiki/,
["Vimwiki"],
"pandoc -f markdown-tex_math_dollars-raw_tex",
:vimwiki)
# bind your own extension regex (the new set of extensions will also include `.asc` and `.adoc`):
# # * file reference: /var/lib/gems/2.1.0/gems/github-markup-1.6.0/lib/github/markups.rb
Gollum::Markup.register(:vimwiki, "Vimwiki")
Gollum::Markup.formats[:vimwiki][:regexp] = /vimwiki/
GitHub::Markup::markup_impl(:vimwiki, ci)
##################
Attempt to replace the Markdown primary extension and regex.
I don't understand why this isn't working unless I'm misunderstanding the order of operations for matching and overrides.
The page will display, the extension is recognized, but the page does not format at all -scrunches everything together.
# always include this:
Gollum::Page.send :remove_const, :FORMAT_NAMES if defined? Gollum::Page::FORMAT_NAMES
# Attempt to replace the primary extension for Markdown
# remove the original markdown binding:
Gollum::Markup.formats.delete(:markdown)
# and define your own
Gollum::Markup.formats[:thing] = {
:name => "Markdown",
:regexp => /thing/
}
Attempt to just replace the markdown extension regex
Same result as attempt 2.
The page will display but it is not formatted correctly
# always include this:
Gollum::Page.send :remove_const, :FORMAT_NAMES if defined? Gollum::Page::FORMAT_NAMES
Gollum::Markup.formats[:markdown][:regexp] = /vimwiki|thing/
I'm closing the question:
As it turns out there is no way easy way to use the markdown processor and also get the edit page to recognize that there is a different extension. The markdown processor requires the extension to be valid it it's regex of extensions found in markdown.rb.
The bottom line is that I would need to implement something similar to what pandoc is doing anyway. So I'm just going just drop the subject and stick with pandoc -'Things I've tried #1' from the original question.
Rather than take a bunch of space here I've posted the documentation on the project page with code examples
Thank you to whomever spent some time thinking about this.

Can't Load my Ruby File

OK SO I Am just picking Ruby up pretty much for the kicks and giggles... and Believe me when I say I'm stumped.
I want to create a bot for my Twitch stream and do it in Ruby because I found a fairly easy tut to follow along with, along with my reasoning skills. However I'm having a very hard time getting my command prompt or pry to load the file.
Here is my file JUST IN CASE
require 'socket'
TWITCH_HOST = "irc.twitch.tv"
TWITCH_PORT = 6667
class Fox
def initialize
#nickname = "mybotsname"
#password = "I have the proper oauth here"
#channel = "mytwitchchannel"
#socket = TCPSocket.open(TWITCH_HOST, TWITCH_PORT)
write_to_system "PASS #{#password}"
write_to_system "NICK #{#nickname}"
write_to_system "USER #{#nickname} 0 * #{#nickname}"
write_to_system "JOIN ##{#Channel}"
end
def write_to_system(message)
#socket.puts message
end
def write_to_chat(message)
write_to_system "PRIVMSG ##{#channel} :{message}"
end
end
Now, From what I gathered, I should beable to go into my command prompt and type pry
I get this.
Pry
Now, I want to run my program which is located in a dropbox (Private use)
I'm Still very new to the concept of Repl's as I've been working with Java mostly along with very LITTLE Experience in other languages. What am I doing wrong here? Why can I not get my file to load properly? I've also tried filepathing and got this.FilePathing
I'm sorry if this is a stupid question. It's just driving me absolutely bat-brain crazy. The reason this is driving me bonkers is the video I was watching, he didn't do anything different other than my guess is he was using Terminal instead of Command Prompt. I Wanted originally to do this through Cygwin but upon install of Pry I lost a bunch of Cygwin files and can no longer load Cygwin, I will re-install the over all program later and see what I can from there.
Sorry for no embedded pics.
Also, any easier way to do this I'm all ears. I've tried Komodo Edit 10 but it's not playing nice ether.
Require from LOAD_PATH
A Ruby module or class file needs to be in the LOAD_PATH to require it with Kernel#require. For example, if your file is named just_in_case.rb, you can use:
$LOAD_PATH.unshift '/path/to/dropbox/directory'
# Leave off the path and .rb extension.
require 'just_in_case'
Load from an absolute path
If you need to provide an absolute path, then you should use Kernel#load instead. For example:
# Use the absolute path and the .rb extension.
load '/path/to/dropbox/just_in_case.rb'
Caveats
There are some other differences in behavior between require, require_relative, and load, but they probably don't really matter within the limited scope of the question you asked except that there have historically been issues with Kernel#require_relative within the REPL. It may or may not work as expected now, but I would still recommend require or load for your specific use case.

(Unit)test pdf generation

We implemented a magento module https://github.com/firegento/firegento-pdf/ and I plan to write tests for the module.
The problem is: The extension generates pdfs.
Is there any framework, or whatever to test pdfs? It would be totally fine if I can check for text in the pdf. I don't want to check the correct placement.
Andy ideas?
This looks promising but feels oversized. http://webcheatsheet.com/php/reading_clean_text_from_pdf.php
I use PdfBox for a similar module, a Java based command line utility that extracts text from a PDF and optionally converts it to HTML: http://pdfbox.apache.org/commandline/#extractText
To use it within PHPUnit tests, I wrote a PHP interface for the relevant PdfBox methods: https://github.com/schmengler/PdfBox
Example
use SGH\PdfBox;
//$pdf = GENERATED_PDF;
$converter = new PdfBox;
$converter->setPathToPdfBox('/usr/bin/pdfbox-app-1.7.0.jar');
$text = $converter->textFromPdfStream($pdf);
Further reading: Unit Test Generated PDFs with PHPUnit and PDFBox
Maybe you could use Inkscape to convert it into SVG and make asserts on some SVG Nodes.
That would do if you only want to check the text or some simple formatting.
$ inkscape -f invoice.pdf --export-plain-svg=thepdf.svg
For the correct position you need to be a bit fuzzy, though.
Nevertheless the PDF source can be plain enough to be checked with simple strpos().
You have to resign yourself to testing "we sent the right commands to Magento". Testing the output PDF would cause fragility.
Mock the PDF-writing library, and test that your code called the library correctly. This has the added benefit of speed, but requires a little more discipline. If a PDF output fails a manual inspection, you MUST fix that test-first, to keep your mocks honest.

Rendering HAML partials from within HAML outside of Rails

I'm using HAML to generate some static html pages for a site, and I was wanting to split out common components into partials that I can include in multiple pages, just like in Rails. However I don't want to use the whole Rails stack to do this as it seems like overkill.
I've looked around on the Internet but haven't found anything, better than just doing something like:
Haml::Engine.new(IO.read("header.haml")).render
Is there a nicer way of including so-called partials from within HAML? An existing filter or command that I'm missing?
It's best to combine haml & sass with a tool for building static websites. Here's some links:
StaticMatic
Serve
Haml Jekyll -- a fork of Jekyll that supports haml.
Middleman
I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.
Darn, you're right – there isn't a built-in way. I've used helpers with the haml command line, but always ones whose output was already HTML formatted.
My best suggestion is to write a partial() method and require it. It looks like you have already started down this path. I would suggest anyone writing such a function consider keeping the original binding around in some way. Haml::Helpers#capture_hame looks to be the easiest way to make this happen.
If execution speed is an issue, it might also be a good idea to cache some of the parsed template in the same way that Merb does it.
If someone does get some code working, please put it up on GitHub and make a comment here.
I finally found what I was looking for, so I thought I'd share my find with you. you can simply use the same syntax you use for haml in the first place - http://www.semicomplete.com/blog/geekery/partials-in-haml-in-sinatra.html
/views/file.haml
%h3 Title
%div= haml :content, :layout => false
/views/content.haml
%p your content here
All of the solutions seemed bulky for my purposes, though I'm trying to do basically the same thing. Here is a ruby script I wrote that does precious little - evaluates Haml with the option to stick in = partial "test.haml" anywhere you like. It does a trivial amount of logic to try and find a partial file.
require 'haml'
def find filename
return filename if File.exists? filename
path = File.dirname ARGV[0]
filename = path+"/"+filename
return filename if File.exists? filename
throw "Could not find file."
end
def partial filename
source = File.read(find(filename))
engine = Haml::Engine.new(source)
engine.render(binding)
end
puts partial ARGV[0]
you execute it like so : ruby hamlize.rb input.haml > output.html
I had the same problem. I needed to generate some html snippets to optimize the server response, once this snippets have a lot of calculations(for drawing graphs) and every time a user do a request it was doing all the stuff unnecessarily.
Once i have the views partitioned in several haml partials, i wanted to reuse this to generate the html files.
My first approach was trying with the Haml engine, but once i have partials rendering other partials and some functions of the application helper, it didn't worked.
Finality i found a solution that passed by creating a function in the models that i wanted to create an html file ( to_html ) Here is it:
def to_html
ActionView::Base.send :include, ActionView::Helpers::ApplicationHelper
File.open(File.join(Rails.root, "public", 'test.html'), "w") do |file|
file.print ActionView::Base.new(Rails.configuration.paths["app/views"].first).render(
:partial => 'partial_folder/partial',
:format => :html,
:locals => { :model => self}
)
end
end
This way, i was able to use the already done partials, to print the html files.
This worked for me, maybe it can help you also.

Resources