Mercurial > hg > soundsoftware-site
comparison lib/redmine/wiki_formatting/markdown/formatter.rb @ 1517:dffacf8a6908 redmine-2.5
Update to Redmine SVN revision 13367 on 2.5-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:29:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1516:b450a9d58aed | 1517:dffacf8a6908 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program; if not, write to the Free Software | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require 'cgi' | |
19 | |
20 module Redmine | |
21 module WikiFormatting | |
22 module Markdown | |
23 class HTML < Redcarpet::Render::HTML | |
24 include ActionView::Helpers::TagHelper | |
25 | |
26 def link(link, title, content) | |
27 css = nil | |
28 unless link && link.starts_with?('/') | |
29 css = 'external' | |
30 end | |
31 content_tag('a', content.to_s.html_safe, :href => link, :title => title, :class => css) | |
32 end | |
33 | |
34 def block_code(code, language) | |
35 if language.present? | |
36 "<pre><code class=\"#{CGI.escapeHTML language} syntaxhl\">" + | |
37 Redmine::SyntaxHighlighting.highlight_by_language(code, language) + | |
38 "</code></pre>" | |
39 else | |
40 "<pre>" + CGI.escapeHTML(code) + "</pre>" | |
41 end | |
42 end | |
43 end | |
44 | |
45 class Formatter | |
46 def initialize(text) | |
47 @text = text | |
48 end | |
49 | |
50 def to_html(*args) | |
51 html = formatter.render(@text) | |
52 # restore wiki links eg. [[Foo]] | |
53 html.gsub!(%r{\[<a href="(.*?)">(.*?)</a>\]}) do | |
54 "[[#{$2}]]" | |
55 end | |
56 # restore Redmine links with double-quotes, eg. version:"1.0" | |
57 html.gsub!(/(\w):"(.+?)"/) do | |
58 "#{$1}:\"#{$2}\"" | |
59 end | |
60 html | |
61 end | |
62 | |
63 def get_section(index) | |
64 section = extract_sections(index)[1] | |
65 hash = Digest::MD5.hexdigest(section) | |
66 return section, hash | |
67 end | |
68 | |
69 def update_section(index, update, hash=nil) | |
70 t = extract_sections(index) | |
71 if hash.present? && hash != Digest::MD5.hexdigest(t[1]) | |
72 raise Redmine::WikiFormatting::StaleSectionError | |
73 end | |
74 t[1] = update unless t[1].blank? | |
75 t.reject(&:blank?).join "\n\n" | |
76 end | |
77 | |
78 def extract_sections(index) | |
79 sections = ['', '', ''] | |
80 offset = 0 | |
81 i = 0 | |
82 l = 1 | |
83 inside_pre = false | |
84 @text.split(/(^(?:.+\r?\n\r?(?:\=+|\-+)|#+.+|~~~.*)\s*$)/).each do |part| | |
85 level = nil | |
86 if part =~ /\A~{3,}(\S+)?\s*$/ | |
87 if $1 | |
88 if !inside_pre | |
89 inside_pre = true | |
90 end | |
91 else | |
92 inside_pre = !inside_pre | |
93 end | |
94 elsif inside_pre | |
95 # nop | |
96 elsif part =~ /\A(#+).+/ | |
97 level = $1.size | |
98 elsif part =~ /\A.+\r?\n\r?(\=+|\-+)\s*$/ | |
99 level = $1.include?('=') ? 1 : 2 | |
100 end | |
101 if level | |
102 i += 1 | |
103 if offset == 0 && i == index | |
104 # entering the requested section | |
105 offset = 1 | |
106 l = level | |
107 elsif offset == 1 && i > index && level <= l | |
108 # leaving the requested section | |
109 offset = 2 | |
110 end | |
111 end | |
112 sections[offset] << part | |
113 end | |
114 sections.map(&:strip) | |
115 end | |
116 | |
117 private | |
118 | |
119 def formatter | |
120 @@formatter ||= Redcarpet::Markdown.new( | |
121 Redmine::WikiFormatting::Markdown::HTML.new( | |
122 :filter_html => true, | |
123 :hard_wrap => true | |
124 ), | |
125 :autolink => true, | |
126 :fenced_code_blocks => true, | |
127 :space_after_headers => true, | |
128 :tables => true, | |
129 :strikethrough => true, | |
130 :superscript => true, | |
131 :no_intra_emphasis => true | |
132 ) | |
133 end | |
134 end | |
135 end | |
136 end | |
137 end |