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, :inline_restore_redmine_links]

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!, #restore_redmine_links

Methods included from Helpers::URL

#uri_with_link_safe_scheme?, #uri_with_safe_scheme?

Methods included from StringArrayDiff::Diffable

#diff, #patch, #replacenextlarger, #reverse_hash

Methods included from CoreExt::String::Inflections

#with_leading_slash

Methods included from CoreExt::String::Conversions

#to_hours

Constructor Details

#initialize(*args) ⇒ Formatter

Returns a new instance of Formatter.



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

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



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

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



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

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

#to_html(*rules) ⇒ Object



43
44
45
46
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 43

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

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



54
55
56
57
58
59
60
61
62
# File 'lib/redmine/wiki_formatting/textile/formatter.rb', line 54

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