From a7b67ec9dda73d666614c558420a0a1d8ed7dc1d Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Wed, 29 Dec 2010 14:59:21 -0700 Subject: [PATCH 1/7] pull in some github-flavored-md stuff, fix macros, autolinks --- .../wiki_formatter.rb | 124 ++++++++++++++---- 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb index 6d181f7..84c6f9d 100644 --- a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb +++ b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb @@ -93,14 +93,53 @@ def transform_toc( str, rs ) module RedmineMarkdownExtraFormatter class WikiFormatter + include ActionView::Helpers::TagHelper + def initialize(text) @text = text end + # Taken from + # https://github.com/github/github-flavored-markdown/blob/gh-pages/code.rb, + # with modifications. + def gfm(text) + # Extract pre blocks + extractions = {} + text.gsub!(%r{
.*?
}m) do |match| + md5 = Digest::MD5.hexdigest(match) + extractions[md5] = match + "{gfm-extraction-#{md5}}" + end + + # prevent foo_bar_baz from ending up with an italic word in the middle + text.gsub!(/(\w+_\w+_\w[\w_]*)/) do |x| + x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__' + end + + # in very clear cases, let newlines become
tags + text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x| + x.gsub(/^(.+)$/, "\\1 ") + end + + # Insert pre block extractions + text.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do + extractions[$1] + end + + + puts "\n\n\n#{text.inspect}\n\n\n" + + text + end + def to_html(&block) @macros_runner = block - parsedText = BlueFeather.parse(@text) - parsedText = inline_macros(parsedText) + parsedText = @text + parsedText.gsub!("\r\n", "\n") + parsedText = gfm(parsedText) + parsedText = BlueFeather.parse(parsedText) + parsedText = inline_auto_link(parsedText) + parsedText = inline_auto_mailto(parsedText) parsedText = syntax_highlight(parsedText) rescue => e return("
problem parsing wiki text: #{e.message}\n"+
@@ -109,33 +148,6 @@ def to_html(&block)
              "
") end - MACROS_RE = / - (!)? # escaping - ( - \{\{ # opening tag - ([\w]+) # macro name - (\(([^\}]*)\))? # optional arguments - \}\} # closing tag - ) - /x - - def inline_macros(text) - text.gsub!(MACROS_RE) do - esc, all, macro = $1, $2, $3.downcase - args = ($5 || '').split(',').each(&:strip) - if esc.nil? - begin - @macros_runner.call(macro, args) - rescue => e - "
Error executing the #{macro} macro (#{e})
" - end || all - else - all - end - end - text - end - PreCodeClassBlockRegexp = %r{^
\s*\n*(.+?)
}m def syntax_highlight(str) @@ -146,5 +158,59 @@ def syntax_highlight(str) "" } end + + AUTO_LINK_RE = %r{ + ( # leading text + <\w+.*?>| # leading HTML tag, or + [^=<>!:'"/]| # leading punctuation, or + ^ # beginning of line + ) + ( + (?:https?://)| # protocol spec, or + (?:s?ftps?://)| + (?:www\.) # www.* + ) + ( + (\S+?) # url + (\/)? # slash + ) + ((?:>)?|[^\w\=\/;\(\)]*?) # post + (?=<|\s|$) + }x unless const_defined?(:AUTO_LINK_RE) + + # Turns all urls into clickable links (code from Rails). + def inline_auto_link(text) + text.gsub!(AUTO_LINK_RE) do + all, leading, proto, url, post = $&, $1, $2, $3, $6 + if leading =~ /=]?/ + # don't replace URL's that are already linked + # and URL's prefixed with ! !> !< != (textile images) + all + else + # Idea below : an URL with unbalanced parethesis and + # ending by ')' is put into external parenthesis + if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) ) + url=url[0..-2] # discard closing parenth from url + post = ")"+post # add closing parenth to post + end + tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external') + %(#{leading}#{tag}#{post}) + end + end + text + end + + # Turns all email addresses into clickable links (code from Rails). + def inline_auto_mailto(text) + text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do + mail = $1 + if text.match(/]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/) + mail + else + content_tag('a', mail, :href => "mailto:#{mail}", :class => "email") + end + end + text + end end end From 31e0de9c1503d3636facbc13a2a9ee37fe5fb710 Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Thu, 30 Dec 2010 09:01:53 -0700 Subject: [PATCH 2/7] fix escaping in four spaces/tab pre blocks --- lib/redmine_markdown_extra_formatter/wiki_formatter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb index 84c6f9d..53c3232 100644 --- a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb +++ b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb @@ -105,7 +105,7 @@ def initialize(text) def gfm(text) # Extract pre blocks extractions = {} - text.gsub!(%r{
.*?
}m) do |match| + text.gsub!(%r{
.*?
|(?:(?: |\t)[^\n]*\n)+}m) do |match| md5 = Digest::MD5.hexdigest(match) extractions[md5] = match "{gfm-extraction-#{md5}}" From 8b633cf3bffccb1a2594644e58e7691884fdf2f1 Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Wed, 5 Jan 2011 08:46:42 -0700 Subject: [PATCH 3/7] don't throw away the text if it doesn't need to be escaped --- lib/redmine_markdown_extra_formatter/wiki_formatter.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb index 53c3232..91151ab 100644 --- a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb +++ b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb @@ -113,7 +113,11 @@ def gfm(text) # prevent foo_bar_baz from ending up with an italic word in the middle text.gsub!(/(\w+_\w+_\w[\w_]*)/) do |x| - x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__' + if x.split('').sort.to_s[0..1] == '__' + x.gsub('_', '\_') + else + x + end end # in very clear cases, let newlines become
tags From 3dee12d9b7271a98329c6a3591dfca9808dddfb7 Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Tue, 1 Feb 2011 10:58:01 -0700 Subject: [PATCH 4/7] escape underlines even when there's only one in a word --- lib/redmine_markdown_extra_formatter/wiki_formatter.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb index 91151ab..099b65d 100644 --- a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb +++ b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb @@ -111,14 +111,8 @@ def gfm(text) "{gfm-extraction-#{md5}}" end - # prevent foo_bar_baz from ending up with an italic word in the middle - text.gsub!(/(\w+_\w+_\w[\w_]*)/) do |x| - if x.split('').sort.to_s[0..1] == '__' - x.gsub('_', '\_') - else - x - end - end + # escape underline characters that aren't on a word boundary + text.gsub!(%r{([^\s_]+\w+[^\s_]+)}) { |x| x.gsub('_', '\_') } # in very clear cases, let newlines become
tags text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x| From 3fb75fa4c2cb18466b5acf7b5099891cf1f2cb8e Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Mon, 7 Feb 2011 15:01:09 -0700 Subject: [PATCH 5/7] use a dup of the original text --- lib/redmine_markdown_extra_formatter/wiki_formatter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb index 099b65d..9254d61 100644 --- a/lib/redmine_markdown_extra_formatter/wiki_formatter.rb +++ b/lib/redmine_markdown_extra_formatter/wiki_formatter.rb @@ -132,7 +132,7 @@ def gfm(text) def to_html(&block) @macros_runner = block - parsedText = @text + parsedText = @text.dup parsedText.gsub!("\r\n", "\n") parsedText = gfm(parsedText) parsedText = BlueFeather.parse(parsedText) From 10c7add4bd5d318cb82bd22f89d706bbd29b05e7 Mon Sep 17 00:00:00 2001 From: Sam Nguyen Date: Wed, 13 Apr 2011 09:21:17 -0700 Subject: [PATCH 6/7] plugin_asset_path already adds relative_url_root. removing redundancy --- app/helpers/redmine_markdown_extra_formatter/helper.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/helpers/redmine_markdown_extra_formatter/helper.rb b/app/helpers/redmine_markdown_extra_formatter/helper.rb index 353ae07..65bfbe0 100644 --- a/app/helpers/redmine_markdown_extra_formatter/helper.rb +++ b/app/helpers/redmine_markdown_extra_formatter/helper.rb @@ -3,8 +3,7 @@ module Helper unloadable def wikitoolbar_for(field_id) - url = Redmine::Utils.relative_url_root + - Engines::RailsExtensions::AssetHelpers.plugin_asset_path('redmine_markdown_extra_formatter', 'help', 'markdown_extra_syntax.html') + url = Engines::RailsExtensions::AssetHelpers.plugin_asset_path('redmine_markdown_extra_formatter', 'help', 'markdown_extra_syntax.html') help_link = l(:setting_text_formatting) + ': ' + link_to(l(:label_help), url, :onclick => "window.open(\"#{url}\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;") From 7b7ff6b96896dd64e9f8f5f156b80da5527b8cc6 Mon Sep 17 00:00:00 2001 From: Anthony Bush Date: Thu, 16 Feb 2012 11:17:27 -0600 Subject: [PATCH 7/7] Fix help text errors --- assets/help/markdown_extra_syntax.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/help/markdown_extra_syntax.html b/assets/help/markdown_extra_syntax.html index 1471415..cd47d74 100644 --- a/assets/help/markdown_extra_syntax.html +++ b/assets/help/markdown_extra_syntax.html @@ -43,7 +43,7 @@

Wiki Syntax Quick Reference

Underline - <ins>Underline</ins>> + <ins>Underline</ins> Underline @@ -54,7 +54,7 @@

Wiki Syntax Quick Reference

- </cite>Quote</cite> + <cite>Quote</cite> Quote