Skip to content

Commit

Permalink
Small tweaks to Maruku
Browse files Browse the repository at this point in the history
A real speedup could be had by redoing output/to_html.rb
  • Loading branch information
distler committed Aug 8, 2011
1 parent 47996ea commit 201c25c
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 76 deletions.
4 changes: 3 additions & 1 deletion vendor/plugins/maruku/lib/maruku/defaults.rb
Expand Up @@ -47,7 +47,9 @@ module MaRuKu
:latex_cache_file => "blahtex_cache.pstore", # cache file for blahtex filter

:debug_keep_ials => false,
:doc_prefix => ''
:doc_prefix => '',

:ignore_wikilinks => true
}

class MDElement
Expand Down
6 changes: 3 additions & 3 deletions vendor/plugins/maruku/lib/maruku/ext/math/parsing.rb
Expand Up @@ -86,8 +86,8 @@ def is_math_enabled?


# This adds support for \eqref
RegEqrefLatex = /\\eqref\{(\w+)\}/
RegEqPar = /\(eq:(\w+)\)/
RegEqrefLatex = /\\eqref\{(\w+?)\}/
RegEqPar = /\(eq:(\w+?)\)/
RegEqref = Regexp.union(RegEqrefLatex, RegEqPar)

MaRuKu::In::Markdown.register_span_extension(
Expand All @@ -101,7 +101,7 @@ def is_math_enabled?
end)

# This adds support for \ref
RegRef = /\\ref\{(\w*)\}/
RegRef = /\\ref\{(\w*?)\}/
MaRuKu::In::Markdown.register_span_extension(
:chars => [?\\, ?(],
:regexp => RegRef,
Expand Down
125 changes: 68 additions & 57 deletions vendor/plugins/maruku/lib/maruku/input/charsource.rb
Expand Up @@ -33,8 +33,8 @@ class CharSourceDebug; end

# Choose!

CharSource = CharSourceManual # faster! 58ms vs. 65ms
#CharSource = CharSourceStrscan
#CharSource = CharSourceManual # faster! 58ms vs. 65ms
CharSource = CharSourceStrscan # Seems faster on LONG documents (where we care)
#CharSource = CharSourceDebug


Expand Down Expand Up @@ -202,76 +202,87 @@ def describe_pos(buffer, buffer_index)
require 'strscan'

class CharSourceStrscan
include SpanLevelParser
include MaRuKu::Strings


def initialize(s, parent=nil)
@s = StringScanner.new(s)
@parent = parent
end

# Return current char as a FixNum (or nil).
def cur_char
@s.peek(1)[0]
@scanner = StringScanner.new(s)
@size = s.size
end

# Return current char as a FixNum (or nil).
def cur_char; @scanner.peek(1)[0]; end

# Return the next n chars as a String.
def cur_chars(n);
@s.peek(n)
end
def cur_chars(n); @scanner.peek(n); end

# Return the char after current char as a FixNum (or nil).
def next_char;
@s.peek(2)[1]
end
def next_char; @scanner.peek(2)[1]; end

# Return a character as a FixNum, advancing the pointer.
def shift_char; @scanner.getch[0]; end

# Advance the pointer
def ignore_char; @scanner.pos= @scanner.pos + 1; end

# Advance the pointer by n
def ignore_chars(n); @scanner.pos= @scanner.pos + n; end

# Resturn the rest of the string
def current_remaining_buffer; @scanner.rest; end

# Returns true if string matches what we're pointing to
def cur_chars_are(string); @scanner.peek(string.size) == string; end

# Returns true if Regexp r matches what we're pointing to
def next_matches(r); !!@scanner.check(r); end

def read_regexp(r); r.match(@scanner.scan(r)); end

def consume_whitespace; @scanner.skip(/\s*/); end

def describe
len = 75
num_before = [len/2, @scanner.pos].min
num_after = [len/2, @scanner.rest_size].min
num_before_max = @scanner.pos
num_after_max = @scanner.rest_size

def shift_char
(@s.get_byte)[0]
end
num_before = [num_before_max, len-num_after].min
num_after = [num_after_max, len-num_before].min

def ignore_char
@s.get_byte
nil
end
index_start = [@scanner.pos - num_before, 0].max
index_end = [@scanner.pos + num_after, @size].min

def ignore_chars(n)
n.times do @s.get_byte end
nil
end
size = index_end- index_start

def current_remaining_buffer
@s.rest #nil #@buffer[@buffer_index, @buffer.size-@buffer_index]
end
str = @scanner.string[index_start, size]
str.gsub!("\n",'N')
str.gsub!("\t",'T')

def cur_chars_are(string)
cur_chars(string.size) == string
end

def next_matches(r)
len = @s.match?(r)
return !!len
end

def read_regexp(r)
string = @s.scan(r)
if string
return r.match(string)
else
return nil
end
end
if index_end == @size
str += "EOF"
end

pre_s = @scanner.pos-index_start
pre_s = [pre_s, 0].max
pre_s2 = [len-pre_s,0].max
# puts "pre_S = #{pre_s}"
pre =" "*(pre_s)

def consume_whitespace
@s.scan(/\s+/)
nil
end
"-"*len+"\n"+
str + "\n" +
"-"*pre_s + "|" + "-"*(pre_s2)+"\n"+
# pre + "|\n"+
pre + "+--- Byte #{@scanner.pos}\n"+

def describe
describe_pos(@s.string, @s.pos)
end
"Shown bytes [#{index_start} to #{size}] of #{@size}:\n"+
@scanner.string.gsub(/^/, ">")

end
# "CharSource: At character #{@buffer_index} of block "+
# " beginning with:\n #{@buffer[0,50].inspect} ...\n"+
# " before: \n ... #{cur_chars(50).inspect} ... "

end
end

class CharSourceDebug
def initialize(s, parent)
Expand Down
112 changes: 97 additions & 15 deletions vendor/plugins/maruku/lib/maruku/input/parse_span_better.rb
Expand Up @@ -23,7 +23,17 @@

module MaRuKu; module In; module Markdown; module SpanLevelParser
include MaRuKu::Helpers


# Concatenates to a string
class SpanContext_String; end

# Pushes to an Arrary, and then calls #join to output
class SpanContext_Array; end

# You choose...
#SpanContext = SpanContext_Array
SpanContext = SpanContext_String # Seems to be faster

EscapedCharInText =
Set.new [?\\,?`,?*,?_,?{,?},?[,?],?(,?),?#,?.,?!,?|,?:,?+,?-,?>]

Expand All @@ -32,6 +42,8 @@ module MaRuKu; module In; module Markdown; module SpanLevelParser

EscapedCharInInlineCode = [?\\,?`]

IgnoreWikiLinks = MaRuKu::Globals[:ignore_wikilinks]

def parse_lines_as_span(lines, parent=nil)
parse_span_better lines.join("\n"), parent
end
Expand Down Expand Up @@ -60,7 +72,7 @@ def read_span(src, escaped, exit_on_chars, exit_on_strings=nil)
# This is only an optimization which cuts 50% of the time used.
# (but you can't use a-zA-z in exit_on_chars)
if c && ((c>=?a && c<=?z) || ((c>=?A && c<=?Z)))
con.cur_string << src.shift_char
con.push_char src.shift_char
next
end

Expand Down Expand Up @@ -142,6 +154,9 @@ def read_span(src, escaped, exit_on_chars, exit_on_strings=nil)
when ?[
if markdown_extra? && src.next_char == ?^
read_footnote_ref(src,con)
elsif IgnoreWikiLinks && src.next_char == ?[
con.push_char src.shift_char
con.push_char src.shift_char
else
read_link(src, con)
end
Expand Down Expand Up @@ -191,7 +206,7 @@ def read_span(src, escaped, exit_on_chars, exit_on_strings=nil)
# or 2) the last char was a space
# or 3) the current string is empty
#if con.elements.empty? ||
if (con.cur_string =~ /\s\Z/) || (con.cur_string.size == 0)
if con.is_end?
# also, we check the next characters
follows = src.cur_chars(4)
if follows =~ /^\_\_\_[^\s\_]/
Expand Down Expand Up @@ -678,16 +693,15 @@ def read_image(src, con)
end # read link
class SpanContext
class SpanContext_Array
include MaRuKu::Strings
# Read elements
attr_accessor :elements
attr_accessor :cur_string
def initialize
@elements = []
@cur_string = ""
@cur_string_array = []
end
def push_element(e)
Expand All @@ -703,17 +717,86 @@ def push_element(e)
def push_elements(a)
for e in a
if e.kind_of? String
e.each_byte do |b| push_char b end
@cur_string_array << e # e.each_byte do |b| push_char b end
else
push_element e
end
end
end
def is_end?
@cur_string_array.empty? || @cur_string_array.last =~ /\s\Z/
end
def push_string_if_present
if @cur_string.size > 0
unless @cur_string_array.empty?
@elements << @cur_string_array.join
@cur_string_array = []
end
nil
end
def push_char(c)
@cur_string_array << c.chr
nil
end
# push space into current string if
# there isn't one
def push_space
last = @cur_string_array.last
@cur_string_array << ' ' unless last =~ /\ \Z/
end
def describe
lines = @elements.map{|x| x.inspect}.join("\n")
s = "Elements read in span: \n" +
lines.gsub(/^/, ' -')+"\n"
s += "Current string: \n #{@cur_string_array.join.inspect}\n" unless @cur_string_array.empty?
s
end
end # SpanContext_Array
class SpanContext_String
include MaRuKu::Strings
# Read elements
attr_accessor :elements
def initialize
@elements = []
@cur_string = ''
end
def push_element(e)
raise "Only MDElement and String, please. You pushed #{e.class}: #{e.inspect} " if
not (e.kind_of?(String) or e.kind_of?(MDElement))
push_string_if_present
@elements << e
nil
end
alias push push_element
def push_elements(a)
for e in a
if e.kind_of? String
@cur_string << e # e.each_byte do |b| push_char b end
else
push_element e
end
end
end
def is_end?
@cur_string.empty? || @cur_string =~ /\s\Z/
end
def push_string_if_present
unless @cur_string.empty?
@elements << @cur_string
@cur_string = ""
@cur_string = ''
end
nil
end
Expand All @@ -726,21 +809,20 @@ def push_char(c)
# push space into current string if
# there isn't one
def push_space
last = @cur_string[@cur_string.size-1]
@cur_string << ?\ if last != ?\
last = @cur_string[@cur_string.length - 1]
@cur_string << ' ' unless last == ?\
end
def describe
lines = @elements.map{|x| x.inspect}.join("\n")
s = "Elements read in span: \n" +
lines.gsub(/^/, ' -')+"\n"
if @cur_string.size > 0
s += "Current string: \n #{@cur_string.inspect}\n"
end
s += "Current string: \n #{@cur_string_array.join.inspect}\n" unless @cur_string_array.empty?
s
end
end # SpanContext
end # SpanContext_String
end end end end # module MaRuKu; module In; module Markdown; module SpanLevelParser

0 comments on commit 201c25c

Please sign in to comment.