Class: BlueFeather::Document
- Inherits:
-
Object
- Object
- BlueFeather::Document
- Defined in:
- lib/bluefeather.rb
Constant Summary collapse
- HEADER_PATTERN =
/^([a-zA-Z0-9-]+?)\s*\:\s*(.+?)\s*(?:\n|\Z)/
- BLANK_LINE_PATTERN =
/^\n/
- HEADER_SEQUEL_PATTERN =
/^\s+(.+)$/
Instance Attribute Summary collapse
-
#body ⇒ Object
(also: #text)
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
Class Method Summary collapse
- .parse(str, default_enc = EncodingType::UTF8) ⇒ Object
- .parse_io(input, default_enc = EncodingType::UTF8) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #css ⇒ Object
- #encoding_type ⇒ Object
-
#initialize(headers = {}, body = '') ⇒ Document
constructor
A new instance of Document.
- #kcode ⇒ Object
- #numbering ⇒ Object (also: #numbering?)
- #numbering_start_level ⇒ Object
- #title ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(headers = {}, body = '') ⇒ Document
Returns a new instance of Document.
286 287 288 289 |
# File 'lib/bluefeather.rb', line 286 def initialize(headers = {}, body = '') @headers = headers @body = body end |
Instance Attribute Details
#body ⇒ Object Also known as: text
Returns the value of attribute body.
195 196 197 |
# File 'lib/bluefeather.rb', line 195 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
195 196 197 |
# File 'lib/bluefeather.rb', line 195 def headers @headers end |
Class Method Details
.parse(str, default_enc = EncodingType::UTF8) ⇒ Object
279 280 281 |
# File 'lib/bluefeather.rb', line 279 def parse(str, default_enc = EncodingType::UTF8) parse_io(StringIO.new(str), default_enc) end |
.parse_io(input, default_enc = EncodingType::UTF8) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/bluefeather.rb', line 200 def parse_io(input, default_enc = EncodingType::UTF8) headers = {} body = nil first_pos = input.pos Util.change_kcode(EncodingType.type_to_kcode(default_enc)){ # default encoding if defined?(Encoding) then input.set_encoding(Encoding.find(default_enc)) end # get headers pos_before_gets = nil first_line = true loop do pos_before_gets = input.pos line = input.gets # cut UTF-8 BOM if first_line and line =~ UTF8_BOM_PATTERN then line.slice!(UTF8_BOM_PATTERN) end first_line = false if line and line.chomp =~ HEADER_PATTERN then key = $1.downcase; value = $2 if key == 'encoding' and not headers.include?('encoding') then kc = EncodingType.type_to_kcode(value.downcase) if input.respond_to?(:set_encoding) then input.set_encoding(value.downcase) # rewind (reason => [ruby-list:45988]) input.pos = first_pos first_line = true else $KCODE = kc end end headers[key] = value else # EOF or Metadata end break end end # back input.pos = pos_before_gets # skip blank lines loop do pos_before_gets = input.pos line = input.gets if line.nil? or not line =~ BLANK_LINE_PATTERN then break end end # back input.pos = pos_before_gets # get body body = input.read } return self.new(headers, body) end |
Instance Method Details
#[](key) ⇒ Object
291 292 293 |
# File 'lib/bluefeather.rb', line 291 def [](key) @headers[key.to_s.downcase] end |
#[]=(key, value) ⇒ Object
295 296 297 |
# File 'lib/bluefeather.rb', line 295 def []=(key, value) @headers[key.to_s.downcase] = value.to_s end |
#css ⇒ Object
303 304 305 |
# File 'lib/bluefeather.rb', line 303 def css @headers['css'] end |
#encoding_type ⇒ Object
327 328 329 |
# File 'lib/bluefeather.rb', line 327 def encoding_type (@headers['encoding'] ? @headers['encoding'].downcase : 'utf-8') end |
#kcode ⇒ Object
331 332 333 |
# File 'lib/bluefeather.rb', line 331 def kcode self.encoding_type && EncodingType.type_to_kcode(self.encoding_type) end |
#numbering ⇒ Object Also known as: numbering?
307 308 309 310 311 312 313 314 |
# File 'lib/bluefeather.rb', line 307 def numbering case @headers['numbering'] when 'yes', '1', 'true', 'on' true else false end end |
#numbering_start_level ⇒ Object
318 319 320 321 322 323 324 325 |
# File 'lib/bluefeather.rb', line 318 def numbering_start_level level = (@headers['numbering-start-level'] || 2).to_i if level >= 1 and level <= 6 then return level else return 2 end end |
#title ⇒ Object
299 300 301 |
# File 'lib/bluefeather.rb', line 299 def title @headers['title'] end |
#to_html ⇒ Object
335 336 337 |
# File 'lib/bluefeather.rb', line 335 def to_html Parser.new.document_to_html(self) end |