Class: MaRuKu::In::Markdown::SpanLevelParser::HTMLHelper

Inherits:
Object
  • Object
show all
Includes:
Strings
Defined in:
lib/maruku/input/html_helper.rb

Overview

I tried to do this with REXML, but wasn’t able to. (suggestions?)

Constant Summary collapse

Tag =
%r{^<(/)?(\w+)\s*([^>]*)>}m
EverythingElse =
%r{^[^<]+}m
CommentStart =
%r{^<!--}x
CommentEnd =
%r{^.*-->}
TO_SANITIZE =
['img','hr']

Constants included from Strings

Strings::Abbreviation, Strings::AttributeDefinitionList, Strings::Definition, Strings::EMailAddress, Strings::FootnoteText, Strings::HeaderWithAttributes, Strings::HeaderWithId, Strings::IncompleteLink, Strings::LinkRegex, Strings::MightBeTableHeader, Strings::Sep, Strings::TabSize, Strings::TableSeparator, Strings::URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Strings

#add_tabs, #dbg_describe_ary, #force_linebreak?, #line_md_type, #normalize_key_and_value, #num_leading_hashes, #number_of_leading_spaces, #parse_email_headers, #spaces_before_first_char, #split_lines, #strip_hashes, #strip_indent, #unquote

Constructor Details

#initializeHTMLHelper

Returns a new instance of HTMLHelper.



40
41
42
43
44
45
46
# File 'lib/maruku/input/html_helper.rb', line 40

def initialize 
	@rest = ""
	@tag_stack = []
	@m = nil
	@already = ""
	@inside_comment = false
end

Instance Attribute Details

#restObject (readonly)

attr_accessor :inside_comment



38
39
40
# File 'lib/maruku/input/html_helper.rb', line 38

def rest
  @rest
end

Instance Method Details

#eat_this(line) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/maruku/input/html_helper.rb', line 48

def eat_this(line)
	@rest = line  + @rest
	things_read = 0
	until @rest.empty?
		if @inside_comment
			if @m = CommentEnd.match(@rest)
				@inside_comment = false
				@already += @m.pre_match + @m.to_s
				@rest = @m.post_match
			elsif @m = EverythingElse.match(@rest)
				@already += @m.pre_match + @m.to_s
				@rest = @m.post_match
			end
		else
			if @m = CommentStart.match(@rest)
				things_read += 1
				@inside_comment = true
				@already += @m.pre_match + @m.to_s
				@rest = @m.post_match
			elsif @m = Tag.match(@rest)
				things_read += 1
				@already += @m.pre_match
				@rest = @m.post_match
			
				is_closing = !!@m[1]
				tag = @m[2]
				attributes = @m[3]
				
				is_single = false
				if attributes =~ /\A(.*)\/\Z/
					attributes = $1
					is_single = true
				end
			
				if TO_SANITIZE.include? tag 
					attributes.strip!
			#		puts "Attributes: #{attributes.inspect}"
					if attributes.size > 0
						@already +=  '<%s %s />' % [tag, attributes]
					else
						@already +=  '<%s />' % [tag]
					end
				elsif is_closing
					@already += @m.to_s
					if @tag_stack.empty?
						error "Malformed: closing tag #{tag.inspect} "+
						      "in empty list"
					end 
					if @tag_stack.last != tag
						error "Malformed: tag <#{tag}> "+
						      "closes <#{@tag_stack.last}>"
					end
					@tag_stack.pop
				elsif not is_single
					@tag_stack.push tag
					@already += @m.to_s
				end
			elsif @m = EverythingElse.match(@rest)
				@already += @m.pre_match + @m.to_s
				@rest = @m.post_match
			else
				error "Malformed HTML: not complete: #{@rest.inspect}"
			end
		end # not inside comment
		
#				puts inspect
#				puts "Read: #{@tag_stack.inspect}"
		break if is_finished? and things_read>0	
	end
end

#error(s) ⇒ Object

Raises:



120
121
122
# File 'lib/maruku/input/html_helper.rb', line 120

def error(s)
	raise Exception, "Error: #{s} \n"+ inspect, caller
end

#inspectObject



124
125
126
127
128
129
130
131
132
# File 'lib/maruku/input/html_helper.rb', line 124

def inspect; "HTML READER\n comment=#{@inside_comment} "+
	"match=#{@m.to_s.inspect}\n"+
	"Tag stack = #{@tag_stack.inspect} \n"+
	"Before:\n"+
	add_tabs(@already,1,'|')+"\n"+
	"After:\n"+
	add_tabs(@rest,1,'|')+"\n"
	
end

#is_finished?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/maruku/input/html_helper.rb', line 139

def is_finished?
	not @inside_comment and @tag_stack.empty?
end

#stuff_you_readObject



135
136
137
# File 'lib/maruku/input/html_helper.rb', line 135

def stuff_you_read
	@already
end