Skip to content

Instantly share code, notes, and snippets.

@thewhodidthis thewhodidthis/builder.rb

Last active Apr 20, 2020
Embed
What would you like to do?
Draft SSG

Poor man's static site builder

\
redcarpet content.md |          # 1. Parse markdown
mustache - data.yaml.mustache | # 2. Save as unescaped HTML in proxy template
mustache - layout.mustache |    # 3. Fill in page layout
tidy                            # 4. Fix indentation
# https://bundler.io/guides/bundler_in_a_single_file_ruby_script.html
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mustache'
gem 'rouge'
gem 'redcarpet'
end
require 'rouge/plugins/redcarpet'
require 'yaml'
# Override default output
class CustomFormatter < Rouge::Formatter
def initialize()
@formatter = Rouge::Formatters::HTML.new
end
def stream(tokens, &b)
# Adapted from,
# https://github.com/rouge-ruby/rouge/blob/master/lib/rouge/formatters/html_pygments.rb#L12
yield "<pre><code>"
@formatter.stream(tokens, &b)
yield "</code></pre>"
end
end
class HTML < Redcarpet::Render::HTML
include Rouge::Plugins::Redcarpet
# Following along with,
# https://github.com/rouge-ruby/rouge/blob/master/lib/rouge/plugins/redcarpet.rb
def rouge_formatter(lexer)
CustomFormatter.new
end
end
# For ideas on how to turn hard coded paths dynamic,
# https://docs.ruby-lang.org/en/2.7.0/OptionParser.html
# https://github.com/mustache/mustache/blob/master/bin/mustache
template = File.read('layout.mustache')
data = YAML.load_file('data.yaml')
view = Mustache.new
# Attach `content` inline instead of sourcing from `data.yaml.mustache`
view[:content] = Redcarpet::Markdown.new(HTML, fenced_code_blocks: true).render(ARGF.read)
# Make available for redirecting
puts view.render(template, data)

Coucou

Hola!

Let's go into business 50/50

alert('coucou');
title: Title
description: Description
title: Hello World!
description: Draft SSG
content: '{{{.}}}'
<!doctype html>
<html lang="en">
<head>
{{#base}}
<base href="{{base}}">
{{/base}}
<meta charset="utf-8">
{{#description}}
<meta name="description" content="{{description}}">
{{/description}}
<title>{{title}}</title>
</head>
<body>
<main>
{{{content}}}
</main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.