Module: Middleman::Vegas::MarkdownParser

Defined in:
lib/middleman-vegas/markdown_parser.rb

Constant Summary collapse

AllOptions =
/([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
LangCaption =
/([^\s]+)\s*(.+)?/i

Class Method Summary collapse

Class Method Details

.get_metadata(markup) ⇒ Object

Extract the metadata from the code block. There are two simple formats:

```LANGUAGE TITLE

```LANGUAGE TITLE URL LINK_TEXT


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/middleman-vegas/markdown_parser.rb', line 44

def self.(markup)
  defaults = { escape: true }
  clean_markup = OptionsParser.new(markup).clean_markup

  if clean_markup =~ AllOptions
    defaults = {
      lang: $1,
      title: $2,
      url: $3,
      link_text: $4,
    }
  elsif clean_markup =~ LangCaption
    defaults = {
      lang: $1,
      title: $2
    }
  end
  OptionsParser.new(markup).parse_markup(defaults)
end

.parse_code_block(code_block) ⇒ Object

Extract from the code block the metadata and the code and highlight it.

```LANGUAGE METADATA
CODE
```


23
24
25
26
27
28
29
30
31
32
# File 'lib/middleman-vegas/markdown_parser.rb', line 23

def self.parse_code_block(code_block)
  code_block.gsub /(\s{0,4})`{3}([^\n]+)?\n(.+?)`{3}\Z/m do
    spacing = Regexp.last_match(1)
    spacing = (spacing == "\n" ? "" : spacing)
     = (Regexp.last_match(2).to_s)
    code = Regexp.last_match(3).to_s
    trimmed_code = code.gsub("\n#{spacing}","\n")[spacing.length..-1]
    "#{spacing}#{::Middleman::Vegas::Highlighter.highlight(trimmed_code, )}"
  end
end

.parse_document(full_document) ⇒ Object

Evaluate the entire document and look code blocks:

```LANGUAGE METADATA
CODE
```


11
12
13
14
15
# File 'lib/middleman-vegas/markdown_parser.rb', line 11

def self.parse_document(full_document)
  full_document.gsub /^\s{0,4}`{3}.+?`{3}/m do |code_block|
    parse_code_block code_block
  end
end