_plugins/stagit.rb (2426B) - raw
1 require 'fileutils'
2 require 'nokogiri'
3
4 def wrap_stagit_output path, header
5 doc = Nokogiri::HTML(File.read(path))
6
7 ## Change git repo name from h1 to h2
8 h = doc.at("h1")
9 h.name = "h2"
10 h.content = "#{h.content}.git"
11
12 ## Put the git repo name, clone URL, and stagit navigation into a container
13 nav_section = doc.create_element "section", class: "container wide git" do |e|
14 e.add_child h
15 e.add_child doc.at(".url")
16 e.add_child doc.at("nav")
17 end
18
19 ## Put the stagit main body into another container
20 main_section = doc.create_element "main", class: "container wide git" do |e|
21 e.add_child doc.at("main").children
22 e.css("h2").each { |h| h.name = "h3" }
23 end
24
25 body = doc.at("body")
26 body.children.remove
27 body << header << nav_section << main_section
28
29 ## Fix the stylesheet and add dummy favion like _layouts/default.md
30 stylesheet = doc.at_css('link[rel="stylesheet"]')
31 stylesheet["href"] = "/stylesheets/stylesheet.css"
32 stylesheet["media"] = "screen"
33 doc.at("head").add_child '<link rel="icon" href="data:image/png;base64,iVBORw0KGgo=">'
34
35 ## Add some nicer markup in commit messages
36 doc.xpath("//pre[b[normalize-space()='commit' and not(preceding-sibling::node())]]//text()").each do |text_node|
37 html = text_node.content
38 ## Turn issue references into links
39 html = html.gsub(/(?<=^|\s)#(\d+)\b/) do |match|
40 "<a href=\"https://github.com/sup-heliotrope/sup/issues/#{$1}\">#{$&}</a>"
41 end if path.include? "/sup/"
42 ## Turn commit references into links
43 html = html.gsub(/(?<=[Cc]ommit )([0-9a-f]+)\b/) do |match|
44 href = Dir.new(File.dirname(path)).each_child.find { |fn| fn.start_with? $& }
45 next $& if href.nil?
46 "<a href=\"#{href}\">#{$&}</a>"
47 end
48 text_node.replace html
49 end
50
51 File.write(path, doc.to_html)
52 end
53
54 Jekyll::Hooks.register :site, :post_write do |site|
55 header = File.read "#{site.source}/_includes/header.html"
56 %w[sup sup-colors sup-website].each do |repo|
57 dest = "#{site.dest}/git/#{repo}"
58 Jekyll.logger.info "Stagit:", "Processing #{repo}"
59
60 FileUtils.mkdir_p dest
61 system "cd #{dest} && stagit -u https://supmua.dev/git/#{repo}/ /home/dan/src/#{repo}" or raise "stagit failed"
62
63 ## Munge stagit generate files to fit the rest of the site
64 Dir.glob "#{dest}/**/*.html", File::FNM_DOTMATCH do |path|
65 wrap_stagit_output path, header unless path.include? "/raw/"
66 end
67 end
68 end