Module: Upmark

Defined in:
lib/upmark.rb,
lib/upmark/errors.rb,
lib/upmark/parser/xml.rb,
lib/upmark/transform/ignore.rb,
lib/upmark/transform_helpers.rb,
lib/upmark/transform/markdown.rb,
lib/upmark/transform/normalise.rb,
lib/upmark/transform/preprocess.rb

Defined Under Namespace

Modules: Parser, Transform, TransformHelpers Classes: ParseFailed

Class Method Summary collapse

Class Method Details

.convert(html) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/upmark.rb', line 11

def self.convert(html)
  xml          = Parser::XML.new
  normalise    = Transform::Normalise.new
  preprocess   = Transform::Preprocess.new
  markdown     = Transform::Markdown.new

  ast = xml.parse(html.strip)
  ast = normalise.apply(ast)
  ast = preprocess.apply(ast)
  ast = markdown.apply(ast)

  # The result is either a String or an Array.
  ast = ast.join if ast.is_a?(Array)

  # Remove trailing whitespace
  ast.gsub!(/ +$/,'')

  # Compress bullet point lists
  ast.gsub!(/^•\s*([^•\n]*)\n+(?=•)/,"* #{'\1'}\n")

  # Any more than two consecutive newline characters is superflous.
  ast.gsub!(/\n(\s*\n)+/, "\n\n")

  # Remove other bullet points
  ast.gsub!(/^•\s*/,"* ")

  ast.strip
rescue Parslet::ParseFailed => e
  raise Upmark::ParseFailed.new('Parse failed', e)
end