|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# |
| 3 | +# JBoss, Home of Professional Open Source |
| 4 | +# Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual |
| 5 | +# contributors by the @authors tag. See the copyright.txt in the |
| 6 | +# distribution for a full listing of individual contributors. |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | + |
| 20 | +require 'rubygems' |
| 21 | +require 'redcarpet' |
| 22 | +require 'nokogiri' |
| 23 | +require 'fileutils' |
| 24 | +require 'pygments.rb' |
| 25 | +require 'rexml/document' |
| 26 | + |
| 27 | +# create a custom renderer that allows highlighting of code blocks |
| 28 | +class HTMLWithPygmentsAndPants < Redcarpet::Render::HTML |
| 29 | + include Redcarpet::Render::SmartyPants |
| 30 | + def block_code(code, language) |
| 31 | + Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'}) |
| 32 | + end |
| 33 | + |
| 34 | + #method copied from: https://gist.github.com/suan/5692767 |
| 35 | + def header(title, level) |
| 36 | + @headers ||= [] |
| 37 | + |
| 38 | + title_elements = REXML::Document.new(title) |
| 39 | + flattened_title = title_elements.inject('') do |flattened, element| |
| 40 | + flattened += if element.respond_to?(:text) |
| 41 | + element.text |
| 42 | + else |
| 43 | + element.to_s |
| 44 | + end |
| 45 | + end |
| 46 | + permalink = flattened_title.downcase.gsub(/[^a-z0-9\s]/, '').gsub(/\W+/, "-") |
| 47 | + |
| 48 | + # for extra credit: implement this as its own method |
| 49 | + if @headers.include?(permalink) |
| 50 | + permalink += "_1" |
| 51 | + # my brain hurts |
| 52 | + loop do |
| 53 | + break if !@headers.include?(permalink) |
| 54 | + # generate titles like foo-bar_1, foo-bar_2 |
| 55 | + permalink.gsub!(/\_(\d+)$/, "_#{$1.to_i + 1}") |
| 56 | + end |
| 57 | + end |
| 58 | + @headers << permalink |
| 59 | + %(\n<h#{level}><a id="#{permalink}" class="anchor" href="##{permalink}"><span class="anchor-icon"></span></a>#{title}</h#{level}>\n) |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | + |
| 64 | +def find(p, tag) |
| 65 | + if p.text |
| 66 | + r = p.text[/^(#{tag}: )(.+)$/, 2] |
| 67 | + if r |
| 68 | + p['id'] = 'metadata' |
| 69 | + return r |
| 70 | + end |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +def find_split(p, tag) |
| 75 | + s = find(p, tag) |
| 76 | + if s |
| 77 | + return s.split(',').sort |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +def metadata(source_path, html) |
| 82 | + # TODO canonicalise path |
| 83 | + toc_file='dist/target/toc.html' |
| 84 | + # Markdown doesn't have an metadata syntax, so all we can do is pray ;-) |
| 85 | + # Look for a paragraph that contains tags, which we define by convention |
| 86 | + page_content = Nokogiri::HTML(html) |
| 87 | + technologies = [] |
| 88 | + level = "" |
| 89 | + prerequisites = [] |
| 90 | + summary = "" |
| 91 | + page_content.css('p').each do |p| |
| 92 | + t = find_split(p, 'Technologies') |
| 93 | + if t |
| 94 | + technologies = t |
| 95 | + end |
| 96 | + l = find(p, 'Level') |
| 97 | + if l |
| 98 | + level = l |
| 99 | + end |
| 100 | + pr = find_split(p, 'Prerequisites') |
| 101 | + if pr |
| 102 | + prerequisites = pr |
| 103 | + end |
| 104 | + s = find(p, 'Summary') |
| 105 | + if s |
| 106 | + summary = s |
| 107 | + end |
| 108 | + |
| 109 | + end |
| 110 | + dir = source_path[/([^\/]+)\/([^\/]+).md$/, 1] |
| 111 | + filename = source_path[/([^\/]+)\/([^\/]+).md$/, 2] |
| 112 | + if dir |
| 113 | + output = "<tr><td align='left'><a href='#{dir}/#{filename}.md' title='#{dir}'>#{dir}</td><td align='left'>#{' '.concat(technologies.map{|u| u} * ', ')}</td><td align='left'>#{summary}</td><td align='left'>#{level}</td><td align='left'>#{' '.concat(prerequisites.map{|u| u} * ', ')}</td></tr>\n" |
| 114 | + FileUtils.mkdir_p(File.dirname(toc_file)) |
| 115 | + File.open(toc_file, 'a').write(output) |
| 116 | + end |
| 117 | +end |
| 118 | + |
| 119 | +def markdown(source_path) |
| 120 | + renderer = HTMLWithPygmentsAndPants.new(optionize [ |
| 121 | + :with_toc_data, |
| 122 | + :xhtml |
| 123 | + ]) |
| 124 | + markdown = Redcarpet::Markdown.new(renderer, optionize([ |
| 125 | + :fenced_code_blocks, |
| 126 | + :no_intra_emphasis, |
| 127 | + :tables, |
| 128 | + :autolink, |
| 129 | + :strikethrough, |
| 130 | + :space_after_headers, |
| 131 | + :with_toc_data |
| 132 | + ])) |
| 133 | + text = source_path.read |
| 134 | + toc_file='dist/target/toc.html' |
| 135 | + if File.exist?(toc_file) |
| 136 | + qs_toc_content=File.open('dist/target/toc.html').read |
| 137 | + qs_toc = "<table><thead><tr><th align='left'><strong>Quickstart Name</strong></th><th align='left'><strong>Demonstrated Technologies</strong></th><th align='left'><strong>Description</strong></th><th align='left'><strong>Experience Level Required</strong></th><th align='left'><strong>Prerequisites</strong></th></tr></thead><tbody>#{qs_toc_content}</table></table>" |
| 138 | + text.gsub!("\[TOC-quickstart\]", qs_toc) |
| 139 | + end |
| 140 | + toc = Redcarpet::Markdown.new(Redcarpet::Render::HTML_TOC).render(text) |
| 141 | + text.gsub!("\[TOC\]", toc) |
| 142 | + rendered = markdown.render(text) |
| 143 | + metadata(source_path.path, rendered) |
| 144 | + rendered = rendered.gsub(/README.md/, "README.html").gsub(/CONTRIBUTING.md/, "CONTRIBUTING.html") |
| 145 | + '<!DOCTYPE html><html><head><title>README</title><link href="http://www.jboss.org/jdf/stylesheets/documentation.css" rel="stylesheet"></link><link href="http://www.jboss.org/jdf/stylesheets/pygments.css" rel="stylesheet"></link></head><body>' + rendered + '</body></html>' |
| 146 | + end |
| 147 | + |
| 148 | +def optionize(options) |
| 149 | + options.each_with_object({}) { |option, memo| memo[option] = true } |
| 150 | +end |
| 151 | + |
| 152 | +puts markdown(ARGF) |
| 153 | + |
0 commit comments