diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc9e5c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Gemfile.lock +.bundle + diff --git a/bbsandbox.rb b/bbsandbox.rb index f1cdcbe..61c7c2b 100644 --- a/bbsandbox.rb +++ b/bbsandbox.rb @@ -3,8 +3,9 @@ require 'slim' require 'digest/sha1' require 'bb-ruby' require 'ruby-bbcode' +require './bristle.rb' -@@engines = ['bb-ruby', 'ruby-bbcode', 'raw'] +@@engines = ['bb-ruby', 'ruby-bbcode', 'raw', 'bristle'] get '/' do @posts = list_posts @@ -35,6 +36,8 @@ def exec_bbcode(engine, body) BBRuby.to_html body when "ruby-bbcode" RubyBBCode.to_html body + when "bristle" + Bristle.parse body when "raw" body else diff --git a/bristle.rb b/bristle.rb new file mode 100644 index 0000000..ceb42c2 --- /dev/null +++ b/bristle.rb @@ -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 diff --git a/bristlecode_nodes.rb b/bristlecode_nodes.rb new file mode 100644 index 0000000..11fdfd1 --- /dev/null +++ b/bristlecode_nodes.rb @@ -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 diff --git a/bristlecode_parser.treetop b/bristlecode_parser.treetop new file mode 100644 index 0000000..1dcb10b --- /dev/null +++ b/bristlecode_parser.treetop @@ -0,0 +1,32 @@ +grammar Bristlecode + rule root + (tag / text)+ + end + + rule text + (!tag .)+ + end + + rule tag + (bold) + end + + ############################## + # bold + ############################## + + rule bold + bold_open + ((!bold_close .)+ ) + bold_close + + end + + rule bold_open + '[b]' / '[B]' + end + + rule bold_close + '[/b]' / '[/B]' + end +end