Class: Riseup::Parser
- Inherits:
-
Object
- Object
- Riseup::Parser
- Defined in:
- lib/riseup/parser.rb
Constant Summary collapse
- @@default_spec =
This is a super basic markup language, none of the characters here are unsafe for HTML
Spec.new([ # Escapes ['\\*', '*'], ['\\=', '='], ['\\`', '`'], ['\\\\', '\\'], # Bold ['**', '<b>', '</b>'], # Italic ['*', '<i>', '</i>'], # Header ['=', '<h1>', '</h2>'], # Code ['`', '<code>', '</code>'], # Newline ["\n", '<br/>'] ])
Instance Method Summary collapse
-
#initialize(unformated, spec = @@default_spec) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #to_s ⇒ Object
- #tokens ⇒ Object
Constructor Details
#initialize(unformated, spec = @@default_spec) ⇒ Parser
Returns a new instance of Parser.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/riseup/parser.rb', line 23 def initialize(unformated, spec = @@default_spec) unless unformated.is_a?(String) raise ArgumentError, 'Unformated text (first argument) must be a String' end unless spec.is_a?(Spec) raise ArgumentError, 'Markup specification (second argument) must be a Riseup::Spec' end @unformated = unformated @spec = spec tokens end |
Instance Method Details
#parse ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/riseup/parser.rb', line 40 def parse tokens if @tokens.nil? token_toggle = Set.new new_html = [] @tokens.each do |t| if @spec.include?(t) if token_toggle.include?(t) token_toggle.delete(t) new_html.append(@spec[t].finish) unless @spec[t].substitute? else if !@spec[t].substitute? token_toggle.add(t) new_html.append(@spec[t].start) else new_html.append(@spec[t].substitute) end end else new_html.append(t) end end # Fix unterminated tokens token_toggle.reverse_each do |token| new_html.append(@spec[token].finish) unless @spec[token].substitute? end new_html.join end |
#to_s ⇒ Object
70 71 72 |
# File 'lib/riseup/parser.rb', line 70 def to_s @tokens.to_s end |
#tokens ⇒ Object
36 37 38 |
# File 'lib/riseup/parser.rb', line 36 def tokens @tokens = @unformated.split(@spec.regex).reject(&:empty?) end |