Module: StaticSiteBuilder

Defined in:
lib/static_site_builder.rb,
lib/static_site_builder/version.rb,
lib/static_site_builder/html_templater.rb

Defined Under Namespace

Classes: HTMLTemplater

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.build_webpage(markdown_filepath, template, output_dirpath = nil) ⇒ Object

Takes a markdown_filepath, reads and converts its contents to html before creating a html file of the same name in the output_dirpath directory. If not provided, the output_dirpath will be the same directory as the markdown file. The output_dirpath will be created if not already. Note: If the html file already exists it’s contents will be overwritten. A template is used to house the html body in, creating a full webpage.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/static_site_builder.rb', line 32

def self.build_webpage(markdown_filepath, template, output_dirpath=nil)
  markdown = File.read(markdown_filepath)

  html_body = self.render(markdown)
  html = template.render(html_body)

  dirpath = File.dirname(markdown_filepath)
  output_dirpath ||= dirpath
  FileUtils.mkdir_p(output_dirpath) unless Dir.exists?(output_dirpath)

  filename_with_md_ext = File.basename(markdown_filepath)
  filename_without_md_ext = filename_with_md_ext.split(".md")[0]
  html_filepath = "#{output_dirpath}/#{filename_without_md_ext}.html"

  File.open(html_filepath, "w") { |f| f.write(html) }
  html_filepath
end

.build_website(markdown_dirpath, template, output_dirpath = nil) ⇒ Object

Takes a markdown_dirpath, finds all “*.md” files and converts each to a “*.html” file in order to build a static website. A template is used to embed each built webpage in. The output_dirpath will default to the markdown_dirpath if not set.



54
55
56
57
58
59
60
61
62
63
# File 'lib/static_site_builder.rb', line 54

def self.build_website(markdown_dirpath, template, output_dirpath=nil)
  html_filepaths = []
  pattern = "#{markdown_dirpath}/*.md"

  Dir.glob(pattern).each do |f|
    html_filepaths << self.build_webpage(f, template, output_dirpath)
  end

  html_filepaths
end

.render(markdown) ⇒ Object

Converts markdown to html and returns it.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/static_site_builder.rb', line 11

def self.render(markdown)
  renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
    tables: true,
    fenced_code_blocks: true,
    autolink: true,
    strikethrough: true,
    superscript: true,
    underline: true,
    highlight: true,
    quote: true,
    footnotes: true
  )
  renderer.render(markdown)
end