forked from olivierlacan/keep-a-changelog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.thor
102 lines (87 loc) · 3.18 KB
/
generate.thor
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'pry'
require 'redcarpet'
class Generate < Thor
include Thor::Actions
desc "index", "generate index.html from README.md"
def index
puts "Processing README.md to generate a new index.html..."
relative_path_to_readme = "README.md"
relative_path_to_index = "index.html"
# `r` means we're using the "read" mode with the file
# we need a String for Redcarpet, it doesn't accept File objects.
string = File.open(relative_path_to_readme, 'r') { |file| file.read }
renderer = HTMLwithHeaderLinks.new
markdown = ::Redcarpet::Markdown.new(renderer, markdown_renderer_options)
rendered_markdown = markdown.render(string)
html_output = template { rendered_markdown }
File.open(relative_path_to_index, 'w') { |file| file.write(html_output) }
puts "All done!"
end
private
def template(&block)
<<-HTML.gsub /^\s+/, ""
<html>
<head>
<title>Keep a Changelog</title>
<link rel="stylesheet" href="assets/stylesheets/normalize.css" media="screen" charset="utf-8">
<link rel="stylesheet" href="assets/stylesheets/style.css" media="screen" charset="utf-8">
<script type="text/javascript" src="//use.typekit.net/tng8liq.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
</head>
<body>
<article>
#{yield}
<footer class="clearfix">
<p class="license">This project is <a href="http://choosealicense.com/licenses/mit/">MIT Licensed</a>.</p>
<p class="about"><a href="https://github.com/olivierlacan/keep-a-changelog/">Typed</a> with trepidation by <a href="http://olivierlacan.com">Olivier Lacan</a> from <a href="http://codeschool.com">Code School</a>.</p>
</footer>
</article>
<script type="text/javascript">
var _gauges = _gauges || [];
(function() {
var t = document.createElement('script');
t.type = 'text/javascript';
t.async = true;
t.id = 'gauges-tracker';
t.setAttribute('data-site-id', '5389808eeddd5b055a00440d');
t.src = '//secure.gaug.es/track.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(t, s);
})();
</script>
</body>
</html>
HTML
end
def markdown_renderer_options
{
autolink: true,
no_intra_emphasis: true,
fenced_code_blocks: true,
disable_indented_code_blocks: true,
lax_spacing: true,
lax_html_blocks: true,
strikethrough: true,
superscript: true,
tables: true,
with_toc_data: true
}
end
end
class HTMLwithHeaderLinks < Redcarpet::Render::HTML
def header(title, level)
@headers ||= []
permalink = title.gsub(/\W+/, '-').downcase
if @headers.include?(permalink)
permalink += '_1'
permalink = permalink.succ while @headers.include?(permalink)
end
@headers << permalink
%(
<h#{level} id=\"#{permalink}\">
<a name="#{permalink}" class="anchor" href="##{permalink}"></a>
#{title}
</h#{level}>
)
end
end