Module: ExtendedMarkdownizer

Defined in:
lib/extended_markdownizer.rb,
lib/extended_markdownizer/version.rb,
lib/generators/extended_markdownizer/install_generator.rb

Defined Under Namespace

Modules: DSL, Generators

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.coderay(text, options = {}) ⇒ Object

‘ExtendedMarkdownizer.coderay` method parses a code block delimited from `code ruby %` until `endcode %` and replaces it with appropriate classes for code highlighting. It can take many languages aside from Ruby.

With a hash of options you can specify ‘:line_numbers` (`:table` or `:inline`), and the class of the enclosing div with `:enclosing_class`.

It also parses a couple of special idioms:

* {% caption 'my caption' %} introduces an h5 before the code and passes
  the caption to the enclosing div as well.

* {% highlight [1,2,3] %} highlights lines 1, 2 and 3. It accepts any
  Enumerable, so you can also give a Range (1..3).


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/extended_markdownizer.rb', line 111

def coderay(text, options = {})
  text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
    options.delete(:highlight_lines)
    options.delete(:caption)

    enclosing_class = options[:enclosing_class] || 'markdownizer_code'

    code, language = $2.strip, $1.strip

    # Mark comments to avoid conflicts with Header parsing
    code.gsub!(/(#+)/) do
      '\\' + $1
    end

    code, options, caption = extract_caption_from(code, options)
    code, options = extract_highlights_from(code, options)

    html_caption = caption ? '<h5>' << caption << '</h5>' : nil

    "<div class=\"#{enclosing_class}#{caption ? "\" caption=\"#{caption}" : ''}\">" << 
      (html_caption || '') <<
        CodeRay.scan(code, language).div({:css => :class}.merge(options)) <<
          "</div>"
  end
end

.detect_images(text) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/extended_markdownizer.rb', line 167

def detect_images(text)
  text.gsub(/^(((http|https):\/\/)?)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix) do |url|
    uri = url
    if ($1 != 'http://')
      uri = 'http://' + url
    end
    # Let's check if it's an image
    if FastImage.type(url) != nil
      return '<img src="'+uri+'">'
    else
      return "<a target=\"_blank\" href=\"#{uri}\">#{url.capitalize}</a>"
    end
    
  end
  
end

.markdown(text, hierarchy = 0) ⇒ Object

‘ExtendedMarkdownizer.markdown` method converts plain Markdown text to formatted html. To parse the markdown in a coherent hierarchical context, you must provide it with the current hierarchical level of the text to be parsed.



88
89
90
91
92
93
94
# File 'lib/extended_markdownizer.rb', line 88

def markdown(text, hierarchy = 0)
  text.gsub! %r[^(\s*)(#+)(.+)$] do
    $1 << ('#' * hierarchy) << $2 << $3
  end
  text.gsub!("\\#",'#')
  RDiscount.new(text).to_html
end

.urls_into_anchors(text) ⇒ Object

‘ExtendedMarkdownizer.urls_into_anchors` method converts the urls in the text into anchors



138
139
140
141
142
143
144
145
146
147
# File 'lib/extended_markdownizer.rb', line 138

def urls_into_anchors(text)
  text.gsub(/^(((http|https):\/\/)?)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix) do |url|
    if ($1 != 'http://')
      uri = 'http://' + url
      return "<a target=\"_blank\" href=\"#{uri}\">#{url}</a>"
    else
      return "<a target=\"_blank\" href=\"#{url}\">#{url}</a>"
    end
  end
end

.vimeo_embedded_videos(text) ⇒ Object

‘ExtendedMarkdownizer.vimeo_embedded_videos` method converts the vimeo urls into an embedded video



160
161
162
163
164
165
# File 'lib/extended_markdownizer.rb', line 160

def vimeo_embedded_videos(text)
  text.gsub(/^(http:\/\/)?(www.)?vimeo.com\/([a-zA-Z0-9_]+)$/ix) do |url|
    iframe = '<iframe src="http://player.vimeo.com/video/'+ $3.to_s + '?byline=0&amp;portrait=0" width="560" height="315" frameborder="0"></iframe>'
    return iframe
  end
end

.youtube_embedded_videos(text) ⇒ Object

‘ExtendedMarkdownizer.youtube_embedded_videos` method converts the youtube urls into an embedded video Another way to do the same: stackoverflow.com/questions/3552228/ruby-on-rails-get-url-string-parameters/3552311#3552311



152
153
154
155
156
157
# File 'lib/extended_markdownizer.rb', line 152

def youtube_embedded_videos(text)
  text.gsub(/^(http:\/\/)?(www.)?youtube.com\/watch\?v=([a-zA-Z0-9_]+)(&(.*))?$/ix) do |url|
    iframe = '<iframe width="560" height="349" src="http://www.youtube.com/embed/' + $3.to_s + '?rel=0" frameborder="0" allowfullscreen></iframe>'
    return iframe
  end
end