Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jswanner
Created June 4, 2014 01:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jswanner/3845ffee7e41bf8800c9 to your computer and use it in GitHub Desktop.
Save jswanner/3845ffee7e41bf8800c9 to your computer and use it in GitHub Desktop.
require 'redcarpet'
desc "generate index.html from README.md"
file "index.html" => "README.md" do |task|
puts "Processing README.md to generate a new 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(task.prerequisites.first, 'r') { |file| file.read }
markdown = ::Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {})
rendered_markdown = markdown.render(string)
html_output = template { rendered_markdown }
File.open(task.name, 'w') { |file| file.write(html_output) }
puts "All done!"
end
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
@jswanner
Copy link
Author

jswanner commented Jun 4, 2014

Also,

File.open(task.prerequisites.first, 'r') { |file| file.read }
# ...
File.open(task.name, 'w') { |file| file.write(html_output) }

can be change to:

File.read(task.prerequisites.first)
# ...
File.write(task.name, html_output)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment