Class: HParser::Block::SuperPre

Inherits:
Pair
  • Object
show all
Includes:
Collectable, Indent, Hatena, Html, Latex
Defined in:
lib/hparser/block/super_pre.rb,
lib/hparser/html.rb,
lib/hparser/text.rb,
lib/hparser/latex.rb,
lib/hparser/hatena.rb

Overview

Super pre parser.

Constant Summary collapse

@@class_format_prefix =
nil
@@use_pygments =
false

Instance Attribute Summary collapse

Attributes inherited from Pair

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hatena

#to_hatena

Methods included from Indent

#_to_text_, #text_content, #to_text

Methods included from Text

#to_text

Methods included from Html

#escape

Methods inherited from Pair

get, spliter

Constructor Details

#initialize(content, format = nil) ⇒ SuperPre

Returns a new instance of SuperPre.



29
30
31
32
# File 'lib/hparser/block/super_pre.rb', line 29

def initialize(content, format = nil)
  super content
  @format = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



28
29
30
# File 'lib/hparser/block/super_pre.rb', line 28

def format
  @format
end

Class Method Details

.<=>(o) ⇒ Object



34
35
36
# File 'lib/hparser/block/super_pre.rb', line 34

def self.<=>(o)
  -1
end

.class_format_prefixObject



117
118
119
# File 'lib/hparser/html.rb', line 117

def self.class_format_prefix
  @@class_format_prefix
end

.class_format_prefix=(prefix) ⇒ Object



120
121
122
# File 'lib/hparser/html.rb', line 120

def self.class_format_prefix=(prefix)
  @@class_format_prefix = prefix
end

.parse(scanner, context, inlines) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hparser/block/super_pre.rb', line 9

def self.parse scanner,context,inlines

  content = format = nil
  if scanner.scan(/^>\|([A-Za-z0-9]+)?\|\s*?$/)
    lines = []
    format = scanner.matched_pattern[1]
    until scanner.scan(/^\|\|<\s*?$/) do
      str = scanner.scan(/.*/)
      break if !str
      lines << str
    end
    content = lines.join("\n")
  end

  if content then
    SuperPre.new content, format
  end
end

.use_pygmentsObject



124
125
126
# File 'lib/hparser/html.rb', line 124

def self.use_pygments
  @@use_pygments
end

.use_pygments=(use_or_not) ⇒ Object



127
128
129
# File 'lib/hparser/html.rb', line 127

def self.use_pygments=(use_or_not)
  @@use_pygments = use_or_not
end

Instance Method Details

#==(o) ⇒ Object



38
39
40
# File 'lib/hparser/block/super_pre.rb', line 38

def ==(o)
  self.class == o.class and self.content == o.content and self.format == o.format
end

#hatena_filter(c) ⇒ Object



73
74
75
# File 'lib/hparser/hatena.rb', line 73

def hatena_filter c
  ">||\n"+c+"\n||<"
end

#html_tagObject



162
# File 'lib/hparser/html.rb', line 162

def html_tag() 'pre' end

#to_htmlObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hparser/html.rb', line 131

def to_html
  content = html_content.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
  if format && @@use_pygments
    # quick hack language name converter (super pre -> pygments)
    lang = format
    case format
    when "cs"
      lang = "csharp"
    when "lisp"
      lang = "cl"
    when "patch"
      lang = "diff"
    when "vb"
      lang = "vbnet"
    end

    begin
      require 'pygments'
      Pygments.highlight(html_content,
                         :lexer => lang, :options => {:encoding => 'utf-8'})
    rescue LoadError
      require 'albino'
      Albino.new(html_content, lang).colorize
    end
  elsif format
    %(<#{html_tag} class="#{@@class_format_prefix}#{escape(format)}">#{content}</#{html_tag}>)
  else
    %(<#{html_tag}>#{content}</#{html_tag}>)
  end
end

#to_latexObject



95
96
97
98
# File 'lib/hparser/latex.rb', line 95

def to_latex
  content = latex_content  ## not 'super'
  %Q[\\begin{verbatim}\n#{content}\n\\end{verbatim}\n]
end