Class: Wing::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/wing.rb

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ Generator

Returns a new instance of Generator.



55
56
57
# File 'lib/wing.rb', line 55

def initialize(files)
  @files = files
end

Instance Method Details

#bodyObject



105
106
107
108
109
110
111
112
113
# File 'lib/wing.rb', line 105

def body
  require "github/markdown"

  GitHub::Markdown.to_html(content, :markdown) do |code, lang|
    if lang == "diagram"
      %Q|<div class="mermaid">\n#{code}\n</div>\n|
    end
  end
end

#configObject



59
60
61
62
63
64
65
# File 'lib/wing.rb', line 59

def config
  @config ||= if File.exists?("config.yml")
    Config.load(File.read("config.yml"))
  else
    Config.new
  end
end

#contentObject



115
116
117
# File 'lib/wing.rb', line 115

def content
  files.map {|f| File.read(f) }.join
end

#filesObject



97
98
99
# File 'lib/wing.rb', line 97

def files
  config.files || @files
end

#gen_htmlObject



77
78
79
80
81
# File 'lib/wing.rb', line 77

def gen_html
  # Write html to file
  @html_path = File.expand_path(File.join("build", "page.html"))
  File.open(@html_path, "w") {|f| f.write html }
end

#gen_pdfObject



68
69
70
71
72
73
74
75
# File 'lib/wing.rb', line 68

def gen_pdf
  prepare
  gen_html

  # Convert to pdf
  system "phantomjs #{phantom_script_path} file://#{@html_path} #{Shellwords.escape(title)}.pdf A4"
  puts "#{title}.pdf generated"
end

#htmlObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wing.rb', line 119

def html
  replacements = {
    "BODY"    => body,
    "TITLE"   => title
  }

  template = File.read(File.join(TEMPLATES, "main.html"))

  replacements.each do |key, value|
    template.gsub!("[#{key}]", value)
  end

  template
end

#phantom_script_pathObject



93
94
95
# File 'lib/wing.rb', line 93

def phantom_script_path
  File.join(SCRIPTS, "rasterize.js")
end

#prepareObject



83
84
85
86
87
88
89
90
# File 'lib/wing.rb', line 83

def prepare
  # Prepare build dir
  FileUtils.rm_r "build" if File.exists?("build")
  FileUtils.mkdir_p "build"

  # Copy assets
  FileUtils.cp_r ASSETS, File.join("build", "assets")
end

#titleObject



101
102
103
# File 'lib/wing.rb', line 101

def title
  config.title || files.first
end