diff options
Diffstat (limited to '_plugins')
-rw-r--r-- | _plugins/add-link-to-plaintext-code-block.rb | 14 | ||||
-rw-r--r-- | _plugins/generate-pastebin-plaintext-alternate.rb | 39 |
2 files changed, 53 insertions, 0 deletions
diff --git a/_plugins/add-link-to-plaintext-code-block.rb b/_plugins/add-link-to-plaintext-code-block.rb new file mode 100644 index 0000000..461102e --- /dev/null +++ b/_plugins/add-link-to-plaintext-code-block.rb @@ -0,0 +1,14 @@ +# </code></pre></div></div> +Jekyll::Hooks.register :documents, :post_render do |doc| + if doc.output_ext == ".html" + code_block_counter = 1 + doc.output = doc.output.gsub(/(<\/code><\/pre><\/div><\/div>)/) do |match| + res = match + + '<div class="plaintext-link"><a href="' + + "#{doc.url}.#{code_block_counter}.txt" + + '">plaintext</a></div>' + code_block_counter += 1 + res + end + end +end diff --git a/_plugins/generate-pastebin-plaintext-alternate.rb b/_plugins/generate-pastebin-plaintext-alternate.rb new file mode 100644 index 0000000..bf97f44 --- /dev/null +++ b/_plugins/generate-pastebin-plaintext-alternate.rb @@ -0,0 +1,39 @@ +require 'cgi' +CODE_BLOCK = /<td class="rouge-code"><pre>(.*?)<\/pre><\/td>/m + +module Jekyll + class PlainTextGenerator < Generator + safe true + + def generate(site) + site.collections.each do |collection| + _collection_name, collection_documents = collection + collection_documents.docs.each do |document| + n = 1 + Renderer + .new(site, document) # create a renderer for the document + .run # generate the HTML string + .scan(CODE_BLOCK) # match all occurrences of regexp + .each do |code_block| # iterate on each match + unhighlighted_code = code_block[0] # regexp only defines 1 match (only 1 parens) + .gsub(/<span class=".*?">(.*?)<\/span>/m, '\1') + content = CGI.unescapeHTML unhighlighted_code + name = "#{document.url}.#{n}.txt" + plain = PageWithoutAFile.new(site, site.source, "", name) + plain.content = content + site.pages << plain + n += 1 + end + end + end + end + end + + # Taken from: + # https://github.com/jekyll/jekyll-feed/blob/c552b8ef7bd7a4babcfb5aec2b22283a5bc354dd/lib/jekyll-feed/page-without-a-file.rb#L4 + class PageWithoutAFile < Jekyll::Page + def read_yaml(*) + @data ||= {} + end + end +end |