Class: PseudoHiki::PageComposer

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

Defined Under Namespace

Classes: BaseComposer, GfmComposer, HtmlComposer, PlainComposer

Constant Summary collapse

HEADING_WITH_ID_PAT =
/^(!{2,3})\[([A-Za-z][0-9A-Za-z_\-.:]*)\]\s*/o
PlainFormat =
PlainTextFormat.create

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PageComposer

Returns a new instance of PageComposer.



175
176
177
178
# File 'lib/pseudohiki/converter.rb', line 175

def initialize(options)
  @options = options
  @composer = select_composer.new(options)
end

Instance Method Details

#choose_template(main, body, current_binding) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pseudohiki/converter.rb', line 209

def choose_template(main, body, current_binding)
  if @options[:template]
    html = ERB.new(@options.read_template_file).result(current_binding)
  else
    html = @options.create_html_template_with_current_options
    embed_css = @options[:embed_css]
    html.embed_style(File.read(File.expand_path(embed_css))) if embed_css
    html.push main || body
    html.add_skip_link if html.kind_of?(HtmlTemplate) and main
  end

  html
end

#compose_html(input_lines) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/pseudohiki/converter.rb', line 198

def compose_html(input_lines)
  h1 = split_main_heading(input_lines)
  css = @options[:css]
  tree = BlockParser.parse(input_lines)
  toc = create_table_of_contents(tree)
  body = @composer.compose_body(tree)
  title = @options.title
  main = @composer.create_main(toc, body, h1)
  choose_template(main, body, binding)
end

#create_table_of_contents(tree) ⇒ Object



185
186
187
188
# File 'lib/pseudohiki/converter.rb', line 185

def create_table_of_contents(tree)
  return "" unless @options[:toc]
  @composer.create_table_of_contents(tree)
end

#select_composerObject



180
181
182
183
# File 'lib/pseudohiki/converter.rb', line 180

def select_composer
  return GfmComposer if @options[:html_version].version == "gfm"
  @options.html_template ? HtmlComposer : PlainComposer
end

#split_main_heading(input_lines) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/pseudohiki/converter.rb', line 190

def split_main_heading(input_lines)
  return "" unless @options[:split_main_heading]
  h1_pos = input_lines.find_index {|line| /^![^!]/o.match? line }
  return "" unless h1_pos
  tree = BlockParser.parse([input_lines.delete_at(h1_pos)])
  @options.formatter.format(tree)
end