Skip to content

Commit

Permalink
Converted scanners to class methods & class variables. Nearly works.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber committed Mar 12, 2009
1 parent b5dc7e5 commit fb80e18
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 423 deletions.
2 changes: 1 addition & 1 deletion ext/redcloth_scan/redcloth.h
Expand Up @@ -62,7 +62,7 @@ VALUE red_pass_code(VALUE, VALUE, VALUE, ID);
#define PARSE_ATTR(A) red_parse_attr(self, regs, ID2SYM(rb_intern(A)))
#define PARSE_LINK_ATTR(A) red_parse_link_attr(self, regs, ID2SYM(rb_intern(A)))
#define PARSE_IMAGE_ATTR(A) red_parse_image_attr(self, regs, ID2SYM(rb_intern(A)))
#define PASS_CODE(H, A, T, O) rb_str_append(H, red_pass_code(self, regs, ID2SYM(rb_intern(A)), rb_intern(T)))
#define PASS_CODE(H, A, T) rb_str_append(H, red_pass_code(self, regs, ID2SYM(rb_intern(A)), rb_intern(T)))
#define ADD_BLOCK() \
rb_str_append(html, red_block(self, regs, block, refs)); \
extend = Qnil; \
Expand Down
69 changes: 31 additions & 38 deletions ext/redcloth_scan/redcloth_attributes.rb.rl
Expand Up @@ -13,51 +13,44 @@
}%%

module RedCloth
module RedclothAttributes
include BaseScanner

def write_attributes_machine
%%{
# All other variables become local, letting Ruby garbage collect them. This
# prevents us from having to manually reset them.

variable data @data;
variable p @p;
variable pe @pe;
variable cs @cs;
variable ts @ts;
variable te @te;

write data nofinal;
}%%
end
class RedclothAttributes < BaseScanner
class << self
def redcloth_attribute_parser(machine, data)
@data = data
@regs = {}
@p = 0
@pe = @data.length

def redcloth_attribute_parser(machine, data)
%% write init; #%

@data = data
@regs = {}
@p = 0
@pe = @data.length
%% write init; #%

cs = machine
cs = machine

%% write exec; #%
%% write exec; #%

return @regs
end
return @regs
end

def redcloth_attributes(str)
write_attributes_machine
self.cs = self.redcloth_attributes_en_inline
return redcloth_attribute_parser(cs, str)
end
def redcloth_attributes(str)
self.cs = self.redcloth_attributes_en_inline
return redcloth_attribute_parser(cs, str)
end

def redcloth_link_attributes(str)
write_attributes_machine
self.cs = self.redcloth_attributes_en_link_says;
return redcloth_attribute_parser(cs, str)
def redcloth_link_attributes(str)
self.cs = self.redcloth_attributes_en_link_says;
return redcloth_attribute_parser(cs, str)
end
end

%%{
variable data @data;
variable p @p;
variable pe @pe;
variable cs @cs;
variable ts @ts;
variable te @te;

write data nofinal;
}%%

end
end
4 changes: 2 additions & 2 deletions ext/redcloth_scan/redcloth_common.rl
Expand Up @@ -2,8 +2,8 @@

machine redcloth_common;

action A { @reg = p; }
action B { @bck = p; }
action A { @reg = @p; }
action B { @bck = @p; }
action T { STORE("text"); }
action X { CLEAR_REGS(); RESET_REG(); }
action cat { CAT(block); }
Expand Down
164 changes: 78 additions & 86 deletions ext/redcloth_scan/redcloth_inline.rb.rl
Expand Up @@ -12,109 +12,101 @@

}%%
module RedCloth
module RedclothInline
include BaseScanner

def red_parse_attr(regs, ref)
txt = regs[ref]
new_regs = redcloth_attributes(txt)
return regs.update(new_regs)
end
class RedclothInline < BaseScanner
class << self
def red_parse_attr(regs, ref)
txt = regs[ref.to_sym]
new_regs = redcloth_attributes(txt)
return regs.update(new_regs)
end

def red_parse_link_attr(regs, ref)
txt = regs[ref]
new_regs = red_parse_title(redcloth_link_attributes(txt), ref)
def red_parse_link_attr(regs, ref)
txt = regs[ref.to_sym]
new_regs = red_parse_title(redcloth_link_attributes(txt), ref)

return regs.update(new_regs)
end
return regs.update(new_regs)
end

def red_parse_image_attr(regs, ref)
return red_parse_title(regs, ref);
end
def red_parse_image_attr(regs, ref)
return red_parse_title(regs, ref);
end

def red_parse_title(regs, ref)
# Store title/alt
name = regs[ref]
if ( !name.nil? )
s = name.to_s
p = s.length
if (s[p - 1,1] == ')')
level = -1
p -= 1
while (p > 0 && level < 0) do
case s[p - 1, 1]
when '('; level += 1
when ')'; level -= 1
end
def red_parse_title(regs, ref)
# Store title/alt
name = regs[ref.to_sym]
if ( !name.nil? )
s = name.to_s
p = s.length
if (s[p - 1,1] == ')')
level = -1
p -= 1
end
title = s[p + 1, s.length - 1]
p -= 1 if (p > 0 && s[p - 1, 1] == ' ')
if (p != 0)
regs[ref] = s[0, p]
regs[:title] = title
while (p > 0 && level < 0) do
case s[p - 1, 1]
when '('; level += 1
when ')'; level -= 1
end
p -= 1
end
title = s[p + 1, s.length - 1]
p -= 1 if (p > 0 && s[p - 1, 1] == ' ')
if (p != 0)
regs[ref.to_sym] = s[0, p]
regs[:title] = title
end
end
end
return regs;
end
return regs;
end

def red_pass_code(regs, ref, meth)
txt = regs[ref]
if (!txt.nil?)
txt2 = ""
rb_str_cat_escaped_for_preformatted(txt2, text)
regs[ref] = txt2
def red_pass_code(regs, ref, meth)
txt = regs[ref.to_sym]
if (!txt.nil?)
txt2 = ""
rb_str_cat_escaped_for_preformatted(txt2, text)
regs[ref.to_sym] = txt2
end
return self.call(meth, regs)
end
return self.call(meth, regs)
end

def redcloth_inline(data, refs)
@data = data
@p = 0
@pe = @data.length
write_inline_machine
@orig_data = @data.dup
@block = ""
def redcloth_inline(textile_doc, data, refs)
@textile_doc = textile_doc
@data = data
@refs = refs
@p = 0
@pe = @data.length
@orig_data = @data.dup
CLEAR_REGS()
@block = ""

# From RedclothScan; not sure all are necessary
@refs = refs
@block = ""
CLEAR_REGS()

%% write exec;
##%
%% write init;
%% write exec;
##%

return block
end
return block
end

def redcloth_attributes(str)
return self.clone.extend(RedCloth::RedclothAttributes).redcloth_attributes(str)
end
def redcloth_link_attributes(str)
return self.clone.extend(RedCloth::RedclothAttributes).redcloth_link_attributes(str)
end

def redcloth_attributes(str)
return RedCloth::RedclothAttributes.redcloth_attributes(str)
end
def redcloth_link_attributes(str)
return RedCloth::RedclothAttributes.redcloth_link_attributes(str)
end

def redcloth_inline2(str, refs)
return redcloth_inline(str, refs);
def redcloth_inline2(textile_doc, str, refs)
return redcloth_inline(textile_doc, str, refs);
end
end

def write_inline_machine
%%{
# All other variables become local, letting Ruby garbage collect them. This
# prevents us from having to manually reset them.

variable data @data;
variable p @p;
variable pe @pe;
variable cs @cs;
variable ts @ts;
variable te @te;
%%{
variable data @data;
variable p @p;
variable pe @pe;
variable cs @cs;
variable ts @ts;
variable te @te;

write data nofinal;
write init;
}%%##
end
write data nofinal;
}%%##
end
end
2 changes: 1 addition & 1 deletion ext/redcloth_scan/redcloth_inline.rl
Expand Up @@ -101,7 +101,7 @@
link { PARSE_LINK_ATTR("name"); PASS(block, "name", "link"); };
bracketed_link { PARSE_LINK_ATTR("name"); PASS(block, "name", "link"); };

code { PARSE_ATTR("text"); PASS_CODE(block, "text", "code", opts); };
code { PARSE_ATTR("text"); PASS_CODE(block, "text", "code"); };
code_tag_start { CAT(block); fgoto code_tag; };
notextile { INLINE(block, "notextile"); };
strong { PARSE_ATTR("text"); PASS(block, "text", "strong"); };
Expand Down

0 comments on commit fb80e18

Please sign in to comment.