Class: TracWiki::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/trac-wiki/parser.rb

Constant Summary collapse

MACRO_BEG_REX =

string begins with macro

/\A\{\{ ( \$[\$\.\w]+ | [\#!]\w* |\w+ ) /x
MACRO_BEG_INSIDE_REX =
/\A(.*?)(?<!\{)\{\{ ( \$[\$\.\w]+ | [\#!]\w* | \w+ ) /xm
MACRO_END_REX =

find end of marcro or begin of inner macro

/\A(.*?) ( \}\} | \{\{ ( \$[\$\.\w]+ | [\#!]\w* | \w+)  )/mx

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Parser

Create a new Parser instance.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/trac-wiki/parser.rb', line 127

def initialize(options = nil)
  init_macros
  @macros = true
  @allowed_schemes = %w(http https ftp ftps)
  macro_commands = options.delete :macro_commands
  @macro_commands.merge! macro_commands if ! macro_commands.nil?
  @no_escape = nil
  @base = ''
  options.each_pair {|k,v| send("#{k}=", v) }
  @base += '/' if !@base.empty? && @base[-1] != '/'
end

Instance Attribute Details

#allow_html=(value) ⇒ Object (writeonly)

allow some <b> <form> <html> html will be sanitized



81
82
83
# File 'lib/trac-wiki/parser.rb', line 81

def allow_html=(value)
  @allow_html = value
end

#allowed_schemesObject

Allowed url schemes Examples: http https ftp ftps



45
46
47
# File 'lib/trac-wiki/parser.rb', line 45

def allowed_schemes
  @allowed_schemes
end

#base=(value) ⇒ Object (writeonly)

url base for links



60
61
62
# File 'lib/trac-wiki/parser.rb', line 60

def base=(value)
  @base = value
end

#edit_heading=(value) ⇒ Object (writeonly)

add ‘<a class=’editheading’ href=“?edit=N>edit</a>‘ to each heading



86
87
88
# File 'lib/trac-wiki/parser.rb', line 86

def edit_heading=(value)
  @edit_heading = value
end

#headingsObject

structure where headings are stroed list of hasheses with ‘level` and `title`, `sline` [ { leven: 1, # <h1>

  sline: 3, # line where head starts
  eline: 4, # line before next heading starts
  aname: 'anchor-to-this-heading',
  title: 'heading title'
},
...

]



57
58
59
# File 'lib/trac-wiki/parser.rb', line 57

def headings
  @headings
end

#id_from_heading=(value) ⇒ Object (writeonly)

every heading had id, generated from heading text



99
100
101
# File 'lib/trac-wiki/parser.rb', line 99

def id_from_heading=(value)
  @id_from_heading = value
end

#id_translit=(value) ⇒ Object (writeonly)

when id_from_heading, non ascii char are transliterated to ascii



107
108
109
# File 'lib/trac-wiki/parser.rb', line 107

def id_translit=(value)
  @id_translit = value
end

#macro_commandsObject

like template but more powerfull do no use.



112
113
114
# File 'lib/trac-wiki/parser.rb', line 112

def macro_commands
  @macro_commands
end

#macros=(value) ⇒ Object (writeonly)

use macros? defalut yes



103
104
105
# File 'lib/trac-wiki/parser.rb', line 103

def macros=(value)
  @macros = value
end

#math=(value) ⇒ Object (writeonly)

math syntax extension: $e^x$ for inline math $$ e^x $$ for display math



76
77
78
# File 'lib/trac-wiki/parser.rb', line 76

def math=(value)
  @math = value
end

#merge=(value) ⇒ Object (writeonly)

understand merge tags (see diff3(1)) >>>>>>> mine ||||||| orig

<<<<<<< yours convert to <div class=“merge merge-mine”>mine</div>



95
96
97
# File 'lib/trac-wiki/parser.rb', line 95

def merge=(value)
  @merge = value
end

#no_escape=(value) ⇒ Object (writeonly)

Disable url escaping for local links Escaping: [[/Test]] –> %2FTest No escaping: [[/Test]] –> Test



65
66
67
# File 'lib/trac-wiki/parser.rb', line 65

def no_escape=(value)
  @no_escape = value
end

#no_link=(value) ⇒ Object (writeonly)

Disable url escaping for local links

[whatwerver]

stays [[whatwerver]]



70
71
72
# File 'lib/trac-wiki/parser.rb', line 70

def no_link=(value)
  @no_link = value
end

#template_handlerObject

template_handler(macroname) -> template_text when macros enabled and {myCoolMacro} ocured, result fo ‘template_handler(’myCoolMacro’) inserted



118
119
120
# File 'lib/trac-wiki/parser.rb', line 118

def template_handler
  @template_handler
end

Instance Method Details

#add_macro_command(name, &block) ⇒ Object



170
171
172
# File 'lib/trac-wiki/parser.rb', line 170

def add_macro_command(name, &block)
    @macro_commands[name] = block
end

#allow_html?Boolean

Returns:

  • (Boolean)


82
# File 'lib/trac-wiki/parser.rb', line 82

def allow_html?; @allow_html; end

#edit_heading?Boolean

Returns:

  • (Boolean)


87
# File 'lib/trac-wiki/parser.rb', line 87

def edit_heading?; @edit_heading; end

#id_from_heading?Boolean

Returns:

  • (Boolean)


100
# File 'lib/trac-wiki/parser.rb', line 100

def id_from_heading?; @id_from_heading; end

#id_translit?Boolean

Returns:

  • (Boolean)


108
# File 'lib/trac-wiki/parser.rb', line 108

def id_translit?; @id_translit; end

#macros?Boolean

Returns:

  • (Boolean)


104
# File 'lib/trac-wiki/parser.rb', line 104

def macros?; @macros; end

#make_toc_htmlObject



164
165
166
167
168
# File 'lib/trac-wiki/parser.rb', line 164

def make_toc_html
  @tree = TracWiki::Tree.new
  parse_block(make_toc)
  @tree.to_html
end

#math?Boolean

Returns:

  • (Boolean)


77
# File 'lib/trac-wiki/parser.rb', line 77

def math?; @math; end

#merge?Boolean

Returns:

  • (Boolean)


96
# File 'lib/trac-wiki/parser.rb', line 96

def merge?; @merge; end

#no_escape?Boolean

Returns:

  • (Boolean)


66
# File 'lib/trac-wiki/parser.rb', line 66

def no_escape?; @no_escape; end

#no_link?Boolean

Returns:

  • (Boolean)


71
# File 'lib/trac-wiki/parser.rb', line 71

def no_link?; @no_link; end

#text(text) ⇒ Object



139
140
141
142
# File 'lib/trac-wiki/parser.rb', line 139

def text(text)
  @text = text
  return self
end

#to_html(text = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/trac-wiki/parser.rb', line 146

def to_html(text = nil)
  text(text) if ! text.nil?
  @was_math = false
  @anames = {}
  @count_lines_level = 0
  @text = text if !text.nil?
  @tree = TracWiki::Tree.new
  @edit_heading_class = 'editheading'
  @headings = [ {level: 0, sline: 1 } ]
  @p = false
  @stack = []
  @stacki = []
  @was_math = false
  @line_no = 1
  parse_block(@text)
  @tree.to_html
end

#was_math?Boolean

Returns:

  • (Boolean)


144
# File 'lib/trac-wiki/parser.rb', line 144

def was_math?; @was_math; end