Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added make_*_anchor and make_image to make the converter more customi…

…zable
  • Loading branch information...
commit 13c994811cea6c7694eaaf0ad6320f7ce5ba6e7c 1 parent 9e4cb90
Daniel Mendler authored

Showing 1 changed file with 43 additions and 15 deletions. Show diff stats Hide diff stats

  1. +43 15 lib/creole.rb
58 lib/creole.rb
@@ -22,7 +22,10 @@
22 22 # links will only be allowed if specified using http(s) and ftp(s)
23 23 # schemes. If special link handling is needed, such as inter-wiki or
24 24 # hierachical local links, you must inherit Creole::CreoleParser and
25   -# override make_link.
  25 +# override make_local_link.
  26 +#
  27 +# You can customize the created anchor/image markup by overriding
  28 +# make_*_anchor/make_image.
26 29
27 30 module Creole
28 31
@@ -52,7 +55,7 @@ def self.creolize(creole)
52 55 # each thread that needs to convert Creole to HTML.
53 56 #
54 57 # Inherit this to provide custom handling of links. The overrideable
55   - # methods are: make_link
  58 + # methods are: make_local_link
56 59 class CreoleParser
57 60
58 61 # Create a new CreoleParser instance.
@@ -127,6 +130,22 @@ def start_paragraph
127 130 end
128 131 end
129 132
  133 + # Create anchor markup for direct links. This
  134 + # method can be overridden to generate custom
  135 + # markup, for example to add html additional attributes.
  136 + private
  137 + def make_direct_anchor(uri, text)
  138 + '<a href="' << escape_html(uri) << '">' << escape_html(text) << '</a>'
  139 + end
  140 +
  141 + # Create anchor markup for explicit links. This
  142 + # method can be overridden to generate custom
  143 + # markup, for example to add html additional attributes.
  144 + private
  145 + def make_explicit_anchor(uri, text)
  146 + '<a href="' << escape_html(uri) << '">' << escape_html(text) << '</a>'
  147 + end
  148 +
130 149 # Translate an explicit local link to a desired URL that is
131 150 # properly URL-escaped. The default behaviour is to convert local
132 151 # links directly, escaping any characters that have special
@@ -134,8 +153,8 @@ def start_paragraph
134 153 #
135 154 # Examples:
136 155 #
137   - # make_link("LocalLink") #=> "LocalLink"
138   - # make_link("/Foo/Bar") #=> "%2FFoo%2FBar"
  156 + # make_local_link("LocalLink") #=> "LocalLink"
  157 + # make_local_link("/Foo/Bar") #=> "%2FFoo%2FBar"
139 158 #
140 159 # Must ensure that the result is properly URL-escaped. The caller
141 160 # will handle HTML escaping as necessary. HTML links will not be
@@ -143,10 +162,10 @@ def start_paragraph
143 162 #
144 163 # Example custom behaviour:
145 164 #
146   - # make_link("LocalLink") #=> "/LocalLink"
147   - # make_link("Wikipedia:Bread") #=> "http://en.wikipedia.org/wiki/Bread"
  165 + # make_local_link("LocalLink") #=> "/LocalLink"
  166 + # make_local_link("Wikipedia:Bread") #=> "http://en.wikipedia.org/wiki/Bread"
148 167 private
149   - def make_link(link) #:doc:
  168 + def make_local_link(link) #:doc:
150 169 escape_url(link)
151 170 end
152 171
@@ -183,6 +202,19 @@ def make_image_link(url) #:doc:
183 202 return url
184 203 end
185 204
  205 + # Create image markup. This
  206 + # method can be overridden to generate custom
  207 + # markup, for example to add html additional attributes or
  208 + # to put divs around the imgs.
  209 + private
  210 + def make_image(uri, alt)
  211 + if alt
  212 + '<img src="' << escape_html(uri) << '" alt="' << escape_html(alt) << '"/>'
  213 + else
  214 + '<img src="' << escape_html(uri) << '"/>'
  215 + end
  216 + end
  217 +
186 218 private
187 219 def make_explicit_link(link)
188 220 begin
@@ -192,7 +224,7 @@ def make_explicit_link(link)
192 224 end
193 225 rescue URI::InvalidURIError
194 226 end
195   - return make_link(link)
  227 + return make_local_link(link)
196 228 end
197 229
198 230 def parse_inline(str)
@@ -205,7 +237,7 @@ def parse_inline(str)
205 237 @out << escape_html($2)
206 238 else
207 239 if uri = make_direct_link($2)
208   - @out << '<a href="' << escape_html(uri) << '">' << escape_html($2) << '</a>'
  240 + @out << make_direct_anchor(uri, $2)
209 241 else
210 242 @out << escape_html($&)
211 243 end
@@ -213,7 +245,7 @@ def parse_inline(str)
213 245 when /\A\[\[\s*([^|]*?)\s*(\|\s*(.*?))?\s*\]\]/m
214 246 link = $1
215 247 if uri = make_explicit_link(link)
216   - @out << '<a href="' << escape_html(uri) << '">' << escape_html($3 || link) << '</a>'
  248 + @out << make_explicit_anchor(uri, $3 || link)
217 249 else
218 250 @out << escape_html($&)
219 251 end
@@ -223,11 +255,7 @@ def parse_inline(str)
223 255 @out << '<tt>' << escape_html($1) << '</tt>'
224 256 when /\A\{\{\s*(.*?)\s*(\|\s*(.*?)\s*)?\}\}/ # (|\s*(.*?)\s*)?*\}\}/
225 257 if uri = make_image_link($1)
226   - if $3
227   - @out << '<img src="' << escape_html(uri) << '" alt="' << escape_html($3) << '"/>'
228   - else
229   - @out << '<img src="' << escape_html(uri) << '"/>'
230   - end
  258 + @out << make_image(uri, $3)
231 259 else
232 260 @out << escape_html($&)
233 261 end

0 comments on commit 13c9948

Please sign in to comment.
Something went wrong with that request. Please try again.