Class: Jekyll::Pseudo::Grammar
- Inherits:
-
Object
- Object
- Jekyll::Pseudo::Grammar
- Defined in:
- lib/jekyll-pseudo/grammar.rb
Instance Method Summary collapse
-
#format(txt, brush) ⇒ Object
parse a block of text, using the given brush to format output (works in a single pass).
Instance Method Details
#format(txt, brush) ⇒ Object
parse a block of text, using the given brush to format output (works in a single pass)
5 6 7 8 9 10 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 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 |
# File 'lib/jekyll-pseudo/grammar.rb', line 5 def format(txt, brush) results = [] mappings = [ [/\#(.*$)/, :comment], [/\b([A-Z]\w+)/, :sym], [/(\w+)(?=[({\[])/, :fn], [/(\".*?\")/, :string], [/(<-|->|\+\+|<=|>=|--)/, :op], # try these operators first [/([-()\[\]{}=<>+*])/, :op], # and these second [/\b([a-z][a-zA-Z0-9]*)(_[a-zA-Z0-9]+)?/, :var], [/^(\s+)/, :indent] ] # replace tabs with three spaces txt.gsub! /^\t/, ' ' # count leading whitespace ws = txt.scan(/^ */).map do |leading| leading.size end leading = (ws.min or 0) # remove leading whitespace of the given length if leading > 0 r = /^ {#{leading}}/ txt.gsub! r, '' end # lazy man's parser (we don't do any of that silly backtracking) cursor = 0 while true matches = mappings.map do |pair| [pair[0].match(txt, cursor), pair[1]] end upto = matches.min_by do |pair| matchdata = pair[0] if matchdata == nil txt.size else matchdata.begin(0) end end if upto[0] != nil results << brush.plain(txt.slice(cursor, upto[0].begin(0)-cursor)) # which match? captures = upto[0].captures results << brush.method(upto[1]).call(*captures) cursor = upto[0].end(0) else # no matches remaining results << brush.plain(txt.slice(cursor, txt.size)) break end end return results.join('') end |