treetop parser?

treetop
Jordan Orelli 9 years ago
parent 43d5f93206
commit fc69b7d45e

3
.gitignore vendored

@ -0,0 +1,3 @@
Gemfile.lock
.bundle

@ -3,8 +3,9 @@ require 'slim'
require 'digest/sha1' require 'digest/sha1'
require 'bb-ruby' require 'bb-ruby'
require 'ruby-bbcode' require 'ruby-bbcode'
require './bristle.rb'
@@engines = ['bb-ruby', 'ruby-bbcode', 'raw'] @@engines = ['bb-ruby', 'ruby-bbcode', 'raw', 'bristle']
get '/' do get '/' do
@posts = list_posts @posts = list_posts
@ -35,6 +36,8 @@ def exec_bbcode(engine, body)
BBRuby.to_html body BBRuby.to_html body
when "ruby-bbcode" when "ruby-bbcode"
RubyBBCode.to_html body RubyBBCode.to_html body
when "bristle"
Bristle.parse body
when "raw" when "raw"
body body
else else

@ -0,0 +1,18 @@
require 'sanitize'
require 'treetop'
base_path = File.expand_path(File.dirname(__FILE__))
require File.join(base_path, 'bristlecode_nodes.rb')
class Bristle
Treetop.load(File.join(File.expand_path(File.dirname(__FILE__)), 'bristlecode_parser.treetop'))
@@parser = BristlecodeParser.new
def self.parse(doc)
tree = @@parser.parse(doc)
if tree.nil?
raise Exception, "Bristlecode parse error at offset: #{@@parser.index}"
end
tree
end
end

@ -0,0 +1,37 @@
module Bristlecode
class RootNode < Treetop::Runtime::SyntaxNode
def to_html
elements.each{|elem| elem.to_html}
end
end
class TextNode < Treetop::Runtime::SyntaxNode
def initialize(input, interval, elements)
@text = input[interval]
end
def text_value
@text
end
def inspect(indent="")
"#{indent}TextNode: #{text_value}"
end
def to_html
text_value
end
end
class BoldOpenNode < Treetop::Runtime::SyntaxNode
end
class BoldCloseNode < Treetop::Runtime::SyntaxNode
end
class BoldNode < Treetop::Runtime::SyntaxNode
end
class TagNode < Treetop::Runtime::SyntaxNode
end
end

@ -0,0 +1,32 @@
grammar Bristlecode
rule root
(tag / text)+ <RootNode>
end
rule text
(!tag .)+ <TextNode>
end
rule tag
(bold) <TagNode>
end
##############################
# bold
##############################
rule bold
bold_open
((!bold_close .)+ <TextNode>)
bold_close
<BoldNode>
end
rule bold_open
'[b]' <BoldOpenNode> / '[B]' <BoldOpenNode>
end
rule bold_close
'[/b]' <BoldCloseNode> / '[/B]' <BoldCloseNode>
end
end
Loading…
Cancel
Save