Class: Wing::Generator
- Inherits:
-
Object
- Object
- Wing::Generator
- Defined in:
- lib/wing.rb
Instance Method Summary collapse
- #body ⇒ Object
- #config ⇒ Object
- #content ⇒ Object
- #files ⇒ Object
- #gen_html ⇒ Object
- #gen_pdf ⇒ Object
- #html ⇒ Object
-
#initialize(files) ⇒ Generator
constructor
A new instance of Generator.
- #phantom_script_path ⇒ Object
- #prepare ⇒ Object
- #title ⇒ Object
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
#body ⇒ Object
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 |
#config ⇒ Object
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 |
#content ⇒ Object
115 116 117 |
# File 'lib/wing.rb', line 115 def content files.map {|f| File.read(f) }.join end |
#files ⇒ Object
97 98 99 |
# File 'lib/wing.rb', line 97 def files config.files || @files end |
#gen_html ⇒ Object
77 78 79 80 81 |
# File 'lib/wing.rb', line 77 def gen_html # Write html to file @html_path = File.(File.join("build", "page.html")) File.open(@html_path, "w") {|f| f.write html } end |
#gen_pdf ⇒ Object
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 |
#html ⇒ Object
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_path ⇒ Object
93 94 95 |
# File 'lib/wing.rb', line 93 def phantom_script_path File.join(SCRIPTS, "rasterize.js") end |
#prepare ⇒ Object
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 |
#title ⇒ Object
101 102 103 |
# File 'lib/wing.rb', line 101 def title config.title || files.first end |