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
PartialTag =
%r{^<.*}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::InlineAttributeList, Strings::LinkRegex, Strings::MightBeTableHeader, Strings::Sep, Strings::TabSize, Strings::TableSeparator

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.



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

def initialize 
	@rest = ""
	@tag_stack = []
	@m = nil
	@already = ""
	self.state = :inside_element
end

Instance Attribute Details

#restObject (readonly)

Returns the value of attribute rest.



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

def rest
  @rest
end

#stateObject

:inside_element, :inside_tag, :inside_comment,



49
50
51
# File 'lib/maruku/input/html_helper.rb', line 49

def state
  @state
end

Instance Method Details

#eat_this(line) ⇒ Object



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
# File 'lib/maruku/input/html_helper.rb', line 51

def eat_this(line)
	@rest = line  + @rest
	things_read = 0
	until @rest.empty?
		case self.state
			when :inside_comment
				if @m = CommentEnd.match(@rest)
					@already += @m.pre_match + @m.to_s
					@rest = @m.post_match
					self.state = :inside_element
				else 
					@already += @rest 
					@rest = ""
					self.state = :inside_comment
				end
			when :inside_element 
				if @m = CommentStart.match(@rest)
					things_read += 1
					@already += @m.pre_match + @m.to_s
					@rest = @m.post_match
					self.state = :inside_comment
				elsif @m = Tag.match(@rest) then
					things_read += 1
					handle_tag
					self.state = :inside_element
				elsif @m = PartialTag.match(@rest) then
					@already += @m.pre_match 
					@rest = @m.post_match
					@partial_tag = @m.to_s
					self.state = :inside_tag
				elsif @m = EverythingElse.match(@rest)
					@already += @m.pre_match + @m.to_s
					@rest = @m.post_match
					self.state = :inside_element
				else
					error "Malformed HTML: not complete: #{@rest.inspect}"
				end
			when :inside_tag
				if @m = /^[^>]*>/.match(@rest) then
					@partial_tag += @m.to_s
					@rest = @partial_tag + @m.post_match
					@partial_tag = nil
					self.state = :inside_element
				else
					@partial_tag += @rest
					@rest = ""
					self.state = :inside_tag
				end
			else
				raise "Bug bug: state = #{self.state.inspect}"
		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:



151
152
153
# File 'lib/maruku/input/html_helper.rb', line 151

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

#handle_tagObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/maruku/input/html_helper.rb', line 109

def handle_tag()
	@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

#			puts "READ TAG #{@m.to_s.inspect} tag = #{tag} closing? #{is_closing} single = #{is_single}"
	
	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
	else 
		@already += @m.to_s
		
		@tag_stack.push(tag) unless is_single
	end
end

#inspectObject



155
156
157
158
159
160
161
162
163
# File 'lib/maruku/input/html_helper.rb', line 155

def inspect; "HTML READER\n state=#{self.state} "+
	"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)


170
171
172
# File 'lib/maruku/input/html_helper.rb', line 170

def is_finished?
	(self.state == :inside_element)  and @tag_stack.empty?
end

#stuff_you_readObject



166
167
168
# File 'lib/maruku/input/html_helper.rb', line 166

def stuff_you_read
	@already
end