Class: OrgParse::InlineParser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/org-parse/inline-parser.rb,
lib/org-parse/inline-parser.tab.rb

Overview

行単位の要素を解析する

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
'$end',
'error',
'EX_LOW',
'OTHER',
'EX_HIGH',
'EM_OPEN',
'EM_CLOSE',
'LINK_START',
'LINK_END',
'LINK_URI',
'ST_OPEN',
'ST_CLOSE',
'QUOTE',
'FN_LINK',
'FN_START',
'FN_END',
'IT_OPEN',
'IT_CLOSE',
'UL_OPEN',
'UL_CLOSE',
'VERB_OPEN',
'VERB_CLOSE',
'CODE_OPEN',
'CODE_CLOSE',
'EOL',
'$start',
'content',
'elements',
'element',
'emphasis',
'italic',
'under_line',
'code',
'strike',
'verb',
'normal_string_element',
'link',
'quote',
'fn_link',
'fn_define',
'normal_strings',
'link_descs',
'link_desc']
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bp = nil) ⇒ InlineParser

Returns a new instance of InlineParser.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/org-parse/inline-parser.rb', line 16

def initialize(bp = nil)
  @structp = bp
  @token_que = []
  @footnote_idx = 0
  @footnotes = {}

  # Set up the emphasis regular expression.
  @pre_emphasis = " \t\\('\""
  @post_emphasis = "- \t.,:!?;'\"\\)"
  @border_forbidden = " \t\r\n,\"'"
  @body_regexp = ".*?"
  @markers = "*/_=~+"
  @org_quote_regexp = /@<[^>]+>/
  @org_br_regexp = /\\\\$/
  @org_footnote_regexp = /\[fn:(.+)\]/

  build_org_emphasis_regexp
  build_org_link_regexp
end

Instance Attribute Details

#structpObject

Returns the value of attribute structp.



14
15
16
# File 'lib/org-parse/inline-parser.rb', line 14

def structp
  @structp
end

#token_queObject (readonly)

Returns the value of attribute token_que.



13
14
15
# File 'lib/org-parse/inline-parser.rb', line 13

def token_que
  @token_que
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



405
406
407
# File 'lib/org-parse/inline-parser.tab.rb', line 405

def _reduce_none( val, _values, result )
 result
end

#next_tokenObject



134
135
136
137
138
# File 'lib/org-parse/inline-parser.rb', line 134

def next_token
  return [false, false] if @token_que.empty?
  # p @token_que[0]
  @token_que.shift
end

#on_error(et, ev, values) ⇒ Object

Raises:

  • (ParseError)


140
141
142
143
# File 'lib/org-parse/inline-parser.rb', line 140

def on_error(et, ev, values)
  message = " #{et} #{ev ? ev : ''} #{values}\n"
  raise ParseError, message
end

#parse(src) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/org-parse/inline-parser.rb', line 36

def parse(src)
  return [] if src.empty?
  @src = src
  @token_que = []
  @yydebug = true
  scan(src)
  do_parse
end

#scan(str, verb = false) ⇒ Object

文字列をトークンに分解し、@token_que にセットする



47
48
49
50
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/org-parse/inline-parser.rb', line 47

def scan(str, verb = false)
  symbols = { 
    '*' => [:EM_OPEN, :EM_CLOSE], 
    '/' => [:IT_OPEN, :IT_CLOSE],
    '=' => [:CODE_OPEN, :CODE_CLOSE], 
    '~' => [:VERB_OPEN, :VERB_CLOSE], 
    '+' => [:ST_OPEN, :ST_CLOSE], 
    '_' => [:UL_OPEN, :UL_CLOSE], 
  }
  while !str.empty?
    # puts "str:\"#{str}\""
    matches = []
    matches << [:em, Regexp.last_match] if @org_emphasis_regexp =~ str
    matches << [:lt, Regexp.last_match] if @org_link_text_regexp =~ str
    matches << [:ln, Regexp.last_match] if @org_link_regexp =~ str
    matches << [:quote, Regexp.last_match] if @org_quote_regexp =~ str
    matches << [:br, Regexp.last_match] if @org_br_regexp =~ str
    matches << [:fn, Regexp.last_match] if @org_footnote_regexp =~ str

    if matches.empty?
      @token_que << [:OTHER, str]
      str = ''
    else
      # p matches
      m = matches[0]
      matches.each {|i| m = i if m[1].pre_match.size > i[1].pre_match.size }
      # puts '--------------------'
      # p m[1]
      # puts '--------------------'
      lm = m[1]
      case m[0]
      when :em
        if verb
          pre = lm.pre_match + lm[1] + lm[2]
          @token_que << [:OTHER, pre]
          # puts "#{str};#{pre};#{pre.size}"
          str = str[pre.size..-1]
        else
          str = lm[4]+lm.post_match
          symbol = symbols[lm[2]]
          pre = lm.pre_match + lm[1]
          @token_que << [:OTHER, pre] unless pre.empty?
          @token_que << [symbol[0], symbol[0]]
          v = ['=', '~'].include?(lm[2])
          scan(lm[3], v)
          @token_que << [symbol[1], symbol[1]]
        end
      when :lt
        str = lm.post_match
        pre = lm.pre_match
        @token_que << [:OTHER, pre] unless pre.empty?
        @token_que << [:LINK_START, lm[1]]
        scan lm[2], verb
        @token_que << [:LINK_END, :LINK_END]
      when :ln
        str = lm.post_match
        pre = lm.pre_match
        @token_que << [:OTHER, pre] unless pre.empty?
        @token_que << [:LINK_URI, lm[1]]
      when :quote
        str = lm.post_match
        pre = lm.pre_match
        @token_que << [:OTHER, pre] unless pre.empty?
        @token_que << [:QUOTE, lm[0].sub(/^@/,'')]
      when :br
        str = "\n"
        pre = lm.pre_match
        @token_que << [:OTHER, pre] unless pre.empty?
        @token_que << [:QUOTE, "\n"]
      when :fn
        match, nstr = balanced?(lm[1]+']'+lm.post_match, ['[', ']'])
        pre = lm.pre_match
        if match.empty?
          @token_que << [:OTHER, pre + "[fn:"]
          str = lm[1]+']'+lm.post_match
        else
          str = nstr
          @token_que << [:OTHER, pre] unless pre.empty?
          footnote match
        end
      end
    end
    # p str
    # return if str.nil?
  end
end