Class: HParser::Parser

Inherits:
Object
  • Object
show all
Includes:
Block, Util
Defined in:
lib/hparser/parser.rb

Overview

Block level parser. hparser split hatena format to 2 level.

High-level is block elements. This can be identified by first char.

For exapmle:

* head1
This is block element.(paragpaph)

- list
- is also
-- block element

Low-level is inline elements. Pleease see HParser::Inline::Parser.

Constant Summary

Constants included from Block

Block::Li, Block::Ol, Block::Td, Block::Th, Block::Ul

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Block

make_list_parser

Constructor Details

#initialize(blocks = HParser::Parser.default_parser, inlines = HParser::Inline::Parser.new) ⇒ Parser

Make parser with block parsers and inline parser.

This parser can parse blocks, and parse block content by inlines.

If argument is not gived, this use default_parser.



35
36
37
38
39
40
# File 'lib/hparser/parser.rb', line 35

def initialize(blocks=HParser::Parser.default_parser,
               inlines=HParser::Inline::Parser.new)
  @blocks = Many1.new(Concat.new(Or.new(*blocks),
                                 Skip.new(Empty)))
  @inlines = inlines
end

Class Method Details

.default_parserObject

Retutrn array of all usable parser.

This method collect all classes/modules which include HParser::Block::Collectable. And sorting those by <=>.



60
61
62
# File 'lib/hparser/parser.rb', line 60

def self.default_parser
  AllParser.includes(HParser::Block::Collectable)
end

Instance Method Details

#parse(str) ⇒ Object

Parse hatena format.

Return array of block element.



45
46
47
48
49
50
51
52
53
54
# File 'lib/hparser/parser.rb', line 45

def parse str
  context = Context.new
  res = (@blocks.parse(LineScanner.new(str.split(/\r\n|\r|\n/)),context,@inlines) || []).map{|x|
    x[0]
  }
  if context.footnotes.length > 0
    res << FootnoteList.new(context.footnotes)
  end
  res
end