eprayim 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,8 +6,6 @@ In the beginning when I was a young boy whom had never heard Markdown, it's a ni
6
6
 
7
7
  So the wheel is re-reinvented.
8
8
 
9
- (**Note**: All belows are under construction.)
10
-
11
9
  ## Installation
12
10
 
13
11
  ```
@@ -31,7 +29,7 @@ doc.to_html # => 'hello <em>world</em>'
31
29
  + `**strong**`: **strong**
32
30
  + `_italic_`: *italic*
33
31
  + `~deleted~`: <del>deleted</del>
34
- + `\`inline code\``: `inline code`
32
+ + \`inline code\`: `inline code`
35
33
 
36
34
  And you may use backslash escapes for the following characters:
37
35
 
@@ -4,17 +4,26 @@ require 'eprayim/element.rb'
4
4
  require 'eprayim/parser.rb'
5
5
  require 'eprayim/render.rb'
6
6
  require 'eprayim/render/html.rb'
7
+ require 'stringio'
7
8
 
8
9
  module Eprayim
9
10
  class Doc
10
11
  # the origin text before parse
11
- attr_reader :text
12
+ attr_reader :input
12
13
  attr_reader :parser
13
14
 
14
- def initialize(text)
15
- raise "#{text} should be a string." if not [String, nil].include? text.class
16
- @text = text
17
- @parser = Parser.new(@text)
15
+ def initialize(input)
16
+ @input = input
17
+ @input = StringIO.new(input) if input.is_a? String
18
+ @parser = Parser.new(@input)
19
+ end
20
+
21
+ def title
22
+ @title ||= @parser.peek_head[0]
23
+ end
24
+
25
+ def anchor
26
+ @anchor ||= @parser.peek_head[1]
18
27
  end
19
28
 
20
29
  end
@@ -1,7 +1,11 @@
1
+ require 'stringio'
2
+
1
3
  module Eprayim
2
4
  class Parser
5
+ HEAD_RULE = /\A(?<level>=+)\s*(?<title>[^=#\n]*)(?:#(?<anchor>[^\n]*))?\s*/
6
+
3
7
  BLOCK_RULES = [
4
- [:head, /\A(?<level>=+)\s*(?<title>[^=#\n]*)(?:#(?<anchor>[^\n]*))?\s*/],
8
+ [:head, HEAD_RULE],
5
9
  [:hr, /\A---+\n*/],
6
10
  [:code, /\A```(?<code>.*?)```\s*$/m],
7
11
  [:quote, /\A(>([^\n]*)\n*)+/m],
@@ -21,9 +25,30 @@ module Eprayim
21
25
 
22
26
  # -------------------------
23
27
 
24
- def initialize(text)
25
- raise "#{text} should be a string." if not [String, nil].include? text.class
26
- @text_tail = text.to_s.clone
28
+ def initialize(input)
29
+ @input = if input.nil? or input.is_a? String
30
+ StringIO.new(input.to_s)
31
+ elsif input.respond_to?(:read)
32
+ input.clone
33
+ else
34
+ raise "#{input.inspect} should be a string."
35
+ end
36
+ end
37
+
38
+ # peek the title and anchor without reading
39
+ # all the content.
40
+ def peek_head
41
+ return [@title, @anchor] if @title
42
+ @input.rewind
43
+ @input.each_line do |line|
44
+ m = line.match HEAD_RULE
45
+ if m and m[:level] == '='
46
+ @title = m[:title].strip
47
+ @anchor = m[:anchor].strip
48
+ return [@title, @anchor]
49
+ end
50
+ end
51
+ return [nil, nil]
27
52
  end
28
53
 
29
54
  def parse
@@ -60,6 +85,7 @@ module Eprayim
60
85
  end
61
86
 
62
87
  def parse_block
88
+ @text_tail = @input.read if @text_tail.nil?
63
89
  return nil if @text_tail.empty?
64
90
  BLOCK_RULES.each do |type, regex|
65
91
  m = regex.match(@text_tail)
@@ -4,6 +4,10 @@ require 'lib/eprayim.rb'
4
4
 
5
5
  include Eprayim
6
6
 
7
+ def D(*args)
8
+ Doc.new(*args)
9
+ end
10
+
7
11
  def E(*args)
8
12
  Element.new(*args)
9
13
  end
@@ -5,6 +5,13 @@ class TestParser < MiniTest::Unit::TestCase
5
5
  def setup
6
6
  end
7
7
 
8
+ def test_peek_head
9
+ str = "= h #a"
10
+ assert_equal ['h', 'a'], P(str).peek_head
11
+ str = "\n==\n\n= h #a"
12
+ assert_equal ['h', 'a'], P(str).peek_head
13
+ end
14
+
8
15
  def test_parse_head
9
16
  str = "== h "
10
17
  r = Parser.new(str).parse_block
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eprayim
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-20 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-10-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
14
30
  description: Inspired by markdown, txt2tags and so on.
15
31
  email: me.ssword@gmail.com
16
32
  executables: []