Class: Redmine::WikiFormatting::Textile::Formatter

Inherits:
RedCloth3 show all
Includes:
ActionView::Helpers::TagHelper, LinksHelper
Defined in:
lib/redmine/wiki_formatting/textile/formatter.rb

Constant Summary collapse

RULES =

auto_link rule after textile rules so that it doesn't break !image_url! tags

[:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]

Constants included from LinksHelper

LinksHelper::AUTO_LINK_RE

Constants inherited from RedCloth3

RedCloth3::DEFAULT_RULES, RedCloth3::VERSION

Instance Attribute Summary

Attributes inherited from RedCloth3

#filter_html, #filter_styles, #hard_breaks, #lite_mode, #no_span_caps, #rules

Instance Method Summary collapse

Methods included from LinksHelper

#auto_link!, #auto_mailto!

Methods included from Helpers::URL

#uri_with_safe_scheme?

Methods inherited from String

#is_binary_data?

Methods included from CoreExtensions::String::Inflections

#with_leading_slash

Methods included from CoreExtensions::String::Conversions

#to_hours

Methods included from Diffable

#diff, #patch, #replacenextlarger, #reverse_hash

Constructor Details

#initialize(*args) ⇒ Formatter

Returns a new instance of Formatter

Since:

  • 0.8.0



34
35
36
37
38
39
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 34

def initialize(*args)
  super
  self.hard_breaks=true
  self.no_span_caps=true
  self.filter_styles=false
end

Instance Method Details

#extract_sections(index) ⇒ Object

Since:

  • 1.3.0



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
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 61

def extract_sections(index)
  @pre_list = []
  text = self.dup
  rip_offtags text, false, false
  before = ''
  s = ''
  after = ''
  i = 0
  l = 1
  started = false
  ended = false
  text.scan(/(((?:.*?)(\A|\r?\n\s*\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))?[ \t](.*?)$)|.*)/m).each do |all, content, lf, heading, level|
    if heading.nil?
      if ended
        after << all
      elsif started
        s << all
      else
        before << all
      end
      break
    end
    i += 1
    if ended
      after << all
    elsif i == index
      l = level.to_i
      before << content
      s << heading
      started = true
    elsif i > index
      s << content
      if level.to_i > l
        s << heading
      else
        after << heading
        ended = true
      end
    else
      before << all
    end
  end
  sections = [before.strip, s.strip, after.strip]
  sections.each {|section| smooth_offtags_without_code_highlighting section}
  sections
end

#get_section(index) ⇒ Object

Since:

  • 1.3.0



46
47
48
49
50
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 46

def get_section(index)
  section = extract_sections(index)[1]
  hash = Digest::MD5.hexdigest(section)
  return section, hash
end

#to_html(*rules) ⇒ Object

Since:

  • 0.8.0



41
42
43
44
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 41

def to_html(*rules)
  @toc = []
  super(*RULES).to_s
end

#update_section(index, update, hash = nil) ⇒ Object

Since:

  • 1.3.0



52
53
54
55
56
57
58
59
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 52

def update_section(index, update, hash=nil)
  t = extract_sections(index)
  if hash.present? && hash != Digest::MD5.hexdigest(t[1])
    raise Redmine::WikiFormatting::StaleSectionError
  end
  t[1] = update unless t[1].blank?
  t.reject(&:blank?).join "\n\n"
end