From 6cebae3ffa2ea2b364b0c39238ec7cea243cc2a7 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 17 Aug 2020 09:19:34 -0300 Subject: Generate plaintext links to raw code blocks Generate txt files from code blocks embedded on pages, and add links to them. --- _pastebins/raku-tuple-type-annotation.md | 4 +-- _plugins/add-link-to-plaintext-code-block.rb | 14 ++++++++ _plugins/generate-pastebin-plaintext-alternate.rb | 39 +++++++++++++++++++++++ styles.css | 8 +++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 _plugins/add-link-to-plaintext-code-block.rb create mode 100644 _plugins/generate-pastebin-plaintext-alternate.rb diff --git a/_pastebins/raku-tuple-type-annotation.md b/_pastebins/raku-tuple-type-annotation.md index 9ac62cc..21dbdf2 100644 --- a/_pastebins/raku-tuple-type-annotation.md +++ b/_pastebins/raku-tuple-type-annotation.md @@ -5,7 +5,7 @@ layout: pastebin lang: en --- -```raku +```perl # Single Str return value: this works sub f1(Str $in --> Str) { $in; @@ -24,7 +24,7 @@ sub f2(Str $in --> (Str, Str)) { Error log is: -``` +```perl ===SORRY!=== Error while compiling /path/to/my/file Malformed return value ``` 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 @@ +# +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 + + '' + 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 = /
(.*?)<\/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>/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
diff --git a/styles.css b/styles.css
index 97a3a0f..f350cee 100644
--- a/styles.css
+++ b/styles.css
@@ -136,3 +136,11 @@ span.header-anchor img {
 span.header-anchor:hover img {
   visibility: visible;
 }
+
+/* Plaintext code block links */
+
+div.plaintext-link {
+  margin: auto auto 0 auto;
+  text-align: right;
+  font-family: monospace;
+}
-- 
cgit v1.2.3