aboutsummaryrefslogtreecommitdiff
path: root/_plugins/generate-pastebin-plaintext-alternate.rb
blob: 110a94353ea3e5b312efaf3c59b665629505661c (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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|
          if document.type != :slides
            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
  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 ||= {
        "plaintext" => "true"
      }
    end
  end
end