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
- #header_id_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.
322 323 324 325 326 327 328 |
# File 'lib/bluefeather.rb', line 322 def initialize(headers = {}, body = '') @headers = {} headers.each do |k, v| self[k] = v end @body = body end |
Instance Attribute Details
#body ⇒ Object Also known as: text
Returns the value of attribute body.
230 231 232 |
# File 'lib/bluefeather.rb', line 230 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
230 231 232 |
# File 'lib/bluefeather.rb', line 230 def headers @headers end |
Class Method Details
.parse(str, default_enc = EncodingType::UTF8) ⇒ Object
315 316 317 |
# File 'lib/bluefeather.rb', line 315 def parse(str, default_enc = EncodingType::UTF8) parse_io(StringIO.new(str), default_enc) end |
.parse_io(input, default_enc = EncodingType::UTF8) ⇒ Object
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/bluefeather.rb', line 235 def parse_io(input, default_enc = EncodingType::UTF8) headers = {} body = nil first_pos = input.pos default_enc = EncodingType.regulate(default_enc) Util.change_kcode(EncodingType.convert_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 Util.utf8_bom?(line) 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.convert_to_kcode(value.downcase) if input.respond_to?(:set_encoding) then input.set_encoding(EncodingType.regulate(value)) # 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
330 331 332 |
# File 'lib/bluefeather.rb', line 330 def [](key) @headers[key.to_s.downcase] end |
#[]=(key, value) ⇒ Object
334 335 336 |
# File 'lib/bluefeather.rb', line 334 def []=(key, value) @headers[key.to_s.downcase] = value.to_s end |
#css ⇒ Object
342 343 344 |
# File 'lib/bluefeather.rb', line 342 def css @headers['css'] end |
#encoding_type ⇒ Object
366 367 368 |
# File 'lib/bluefeather.rb', line 366 def encoding_type @headers['encoding'] || EncodingType::UTF8 end |
#header_id_type ⇒ Object
370 371 372 |
# File 'lib/bluefeather.rb', line 370 def header_id_type (@headers['header-id-type'] || HeaderIDType::MD5).downcase end |
#kcode ⇒ Object
374 375 376 |
# File 'lib/bluefeather.rb', line 374 def kcode self.encoding_type && EncodingType.convert_to_kcode(self.encoding_type) end |
#numbering ⇒ Object Also known as: numbering?
346 347 348 349 350 351 352 353 |
# File 'lib/bluefeather.rb', line 346 def numbering case @headers['numbering'] when 'yes', '1', 'true', 'on' true else false end end |
#numbering_start_level ⇒ Object
357 358 359 360 361 362 363 364 |
# File 'lib/bluefeather.rb', line 357 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
338 339 340 |
# File 'lib/bluefeather.rb', line 338 def title @headers['title'] end |
#to_html ⇒ Object
378 379 380 |
# File 'lib/bluefeather.rb', line 378 def to_html Parser.new.document_to_html(self) end |