Class: Moredown

Inherits:
RDiscount show all
Defined in:
lib/moredown.rb

Constant Summary collapse

VERSION =
'1.0.4'

Instance Attribute Summary collapse

Attributes inherited from RDiscount

#filter_html, #filter_styles, #fold_lines, #generate_toc, #smart, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RDiscount

#toc_content

Constructor Details

#initialize(text, args = {}) ⇒ Moredown

Create a Moredown Markdown processor. The text argument should be a string containing Markdown text. Additional arguments may be supplied to set various processing options:

  • :extensions - Any of the following RDiscount Extensions:

    • :smart - Enable SmartyPants processing.

    • :filter_styles - Do not output <style> tags.

    • :filter_html - Do not output any raw HTML tags included in the source text.

    • :fold_lines - RedCloth compatible line folding (not used).

  • :has_stylesheet - Don’t use inline CSS for styling.

  • :emotes - Process emoticons.

  • :base_url - Map any relative URLs in the text to absolute URLs.

  • :map_headings - Map any headings down a few ranks (eg. h1 => h3).

  • :swfobject - Use SwfObject2 if available (eg. :swfobject => { :src => ‘swfobject.js’, :version => ‘10’, :fallback => ‘No Flash’ })

NOTE: The :filter_styles extension is not yet implemented.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/moredown.rb', line 38

def initialize text, args = {}    
  @has_stylesheet = args[:has_stylesheet]
  @emotes = args[:emotes]
  @base_url = args[:base_url]
  @map_headings = args[:map_headings] || 0
  @swfobject = args[:swfobject]
  
  # each swfobject needs a unique id
  @next_flash_id = 0
  
  if args[:extensions]
    super(text, args[:extensions])
  else
    super(text)
  end
end

Instance Attribute Details

#base_urlObject

Map any relative URLs in the text to absolute URLs



13
14
15
# File 'lib/moredown.rb', line 13

def base_url
  @base_url
end

#emotesObject

Process emoticons



10
11
12
# File 'lib/moredown.rb', line 10

def emotes
  @emotes
end

#has_stylesheetObject

Don’t use inline CSS for styling



7
8
9
# File 'lib/moredown.rb', line 7

def has_stylesheet
  @has_stylesheet
end

#map_headingsObject

Map headings down a few ranks (eg. :map_headings => 2 would convert h1 to h3)



16
17
18
# File 'lib/moredown.rb', line 16

def map_headings
  @map_headings
end

#swfobjectObject

Use SwfObject2 if available (eg. :swfobject => { :src => ‘swfobject.js’, :version => ‘10’, :fallback => ‘No Flash’ })



19
20
21
# File 'lib/moredown.rb', line 19

def swfobject
  @swfobject
end

Class Method Details

.text_to_html(text, args = {}) ⇒ Object

Process some text in Markdown format



131
132
133
# File 'lib/moredown.rb', line 131

def self.text_to_html text, args = {}    
  Moredown.new(text, args).to_html
end

Instance Method Details

#to_htmlObject



55
56
57
58
59
60
61
62
63
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/moredown.rb', line 55

def to_html    
  html = super
  
  # flash movies (including youtube)
  html.gsub!(/<img src="(flash|youtube):(.*?)"\s?(?:title="(.*?)")? alt="(.*)" \/>/) do |match|
    # grab width and height
    if $3
      sizes = $3.split(' ')
      sizes = { :width => sizes[0], :height => sizes[1] }
    else
      sizes = {}
    end
    
    # check the source
    if $1.downcase == "youtube"
      url = "http://www.youtube.com/v/#{$2}"
      sizes = { :width => 425, :height => 350 }.merge sizes
    else
      url = $2
    end
    
    flash_tag url, sizes
  end
  
  # image alignments
  if @has_stylesheet
    html.gsub!(/<img (.*?) \/>:(left|right|center)/) { |match| "<img class=\"#{$2}\" #{$1} />" }
  else
    html.gsub!(/<img (.*?) \/>:left/) { |match| "<img style=\"float: left; margin: 0 10px 10px 0;\" #{$1} />" }
    html.gsub!(/<img (.*?) \/>:right/) { |match| "<img style=\"float: right; margin: 0 0 10px 10px;\" #{$1} />" }
    html.gsub!(/<img (.*?) \/>:center/) { |match| "<img style=\"display: block; margin: auto;\" #{$1} />" }
  end
  
  # emoticons
  if @emotes
    html.gsub!(':-)', '<img src="/images/emote-smile.png" alt=":-)" width="16" height="16" />')
    html.gsub!(':-P', '<img src="/images/emote-tongue.png" alt=":-P" width="16" height="16" />')
    html.gsub!(':-D', '<img src="/images/emote-grin.png" alt=":-D" width="16" height="16" />')
    html.gsub!(':-(', '<img src="/images/emote-sad.png" alt=":-(" width="16" height="16" />')
    html.gsub!(':-@', '<img src="/images/emote-angry.png" alt=":-@" width="16" height="16" />')
    html.gsub!(';-)', '<img src="/images/emote-wink.png" alt=";-)" width="16" height="16" />')
  end
  
  # remap relative urls
  if @base_url
    html.gsub!(/<img src="(.*?)"/) do |match|
      if $1.include? '://'
        url = $1
      else
        url = "#{@base_url.chomp('/')}/#{$1}"
      end
      "<img src=\"#{url}\""
    end
    html.gsub!('<a href="/', "<a href=\"#{@base_url}/")
  end
  
  # remap headings down a few notches
  if @map_headings > 0
    html.gsub!(/<(\/)?h(\d)>/) { |match| "<#{$1}h#{$2.to_i + @map_headings}>" }
  end
  
  # add the js for swfobject
  if @swfobject
    swfobjects = []
    1.upto(@next_flash_id).each do |n|
      swfobjects << "swfobject.registerObject(\"swf-#{n}\", \"#{@swfobject[:version]}\");\n"
    end
    
    # html seems to need to go after the swfobject javascript
    html = "<script type=\"text/javascript\" src=\"#{@swfobject[:src]}\"></script><script type=\"text/javascript\">\n/*<![CDATA[*/\n#{swfobjects}/*]]>*/\n</script>\n#{html}"
  end
  
  html
end