Compare commits

...

2 Commits

3
.gitignore vendored

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

@ -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

@ -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,56 @@
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="")
s = "#{indent}TextNode:\n"
text_value.each_line{|line| s += "#{indent} #{line}"}
s
end
def to_html
text_value
end
end
##############################
# bold
##############################
class BoldNode < Treetop::Runtime::SyntaxNode
end
class BoldOpenNode < Treetop::Runtime::SyntaxNode
end
class BoldCloseNode < Treetop::Runtime::SyntaxNode
end
##############################
# italic
##############################
class ItalicNode < Treetop::Runtime::SyntaxNode
end
class ItalicOpenNode < Treetop::Runtime::SyntaxNode
end
class ItalicCloseNode < Treetop::Runtime::SyntaxNode
end
class TagNode < Treetop::Runtime::SyntaxNode
end
end

@ -0,0 +1,51 @@
grammar Bristlecode
rule root
(tag / text)* <RootNode>
end
rule text
(!tag .)+ <TextNode>
end
rule tag
bold / italic
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
##############################
# italic
##############################
rule italic
italic_open
((!italic_close .)+ <TextNode>)
italic_close
<ItalicNode>
end
rule italic_open
'[i]' <ItalicOpenNode> / '[I]' <ItalicOpenNode>
end
rule italic_close
'[/i]' <ItalicCloseNode> / '[/I]' <ItalicCloseNode>
end
end
Loading…
Cancel
Save