Class: BlackCloth

Inherits:
RedCloth
  • Object
show all
Defined in:
lib/bookmaker/blackcloth.rb

Constant Summary collapse

FN_RE =
/
  (\s+)?    # getting spaces
  %\{       # opening
  (.*?)     # footnote
  \}#       # closing
/x
/
  <
  ((?:https?|ftp):\/\/.*?)
  >
/x
@@syntax_blocks =
[]

Instance Method Summary collapse

Instance Method Details

#image_size(img) ⇒ Object



104
105
106
107
# File 'lib/bookmaker/blackcloth.rb', line 104

def image_size(img)
  io = IO.popen("php -r '$info = getimagesize(\"images/#{img}\");echo $info[0].'x'.$info[1];'")
  io.read.split('x').map{|n| n.to_i}
end

#inline(text) ⇒ Object

overriding inline method



110
111
112
113
# File 'lib/bookmaker/blackcloth.rb', line 110

def inline( text ) 
  @rules += [:inline_textile_fn, :inline_textile_url_link]
  super
end

#inline_textile_fn(text) ⇒ Object

Usage: Writing some text with a footnote %is a footnote



12
13
14
15
16
17
18
# File 'lib/bookmaker/blackcloth.rb', line 12

def inline_textile_fn(text)
  text.gsub!( FN_RE )  do |m|
    %(<span class="footnote">#{$2}</span>)
  end
  
  text
end

Usage: <google.com>



27
28
29
30
31
# File 'lib/bookmaker/blackcloth.rb', line 27

def inline_textile_url_link(text)
  text.gsub!( FN_URL_LINK ) do |m|
    %(<a href="#{$1}">#{$1}</a>)
  end
end

#syntax_blocksObject



82
83
84
# File 'lib/bookmaker/blackcloth.rb', line 82

def syntax_blocks
  @@syntax_blocks
end

#textile_figure(tag, attrs, cite, content) ⇒ Object

Usage: figure(This is the caption). some_image.jpg



98
99
100
101
102
# File 'lib/bookmaker/blackcloth.rb', line 98

def textile_figure(tag, attrs, cite, content)
  m, title = *attrs.match(/class="(.*?)"/)
  width, height = image_size(content)
  %(<p class="figure"><img style="width: #{width}px; height: #{height}px" src="../images/#{content}" alt="#{title}" /><br/><span class="caption">#{title}</span></p>)
end

#textile_note(tag, attrs, cite, content) ⇒ Object

Usage: note. Some text



93
94
95
# File 'lib/bookmaker/blackcloth.rb', line 93

def textile_note(tag, attrs, cite, content)
  %(<p class="note">#{content}</p>)
end

#textile_pre(*args) ⇒ Object

Usage: pre. Some code



87
88
89
90
# File 'lib/bookmaker/blackcloth.rb', line 87

def textile_pre(*args)
  # Should I add the default theme as a class?
  send(:textile_syntax, *args)
end

#textile_syntax(tag, attrs, cite, content) ⇒ Object

Usage: syntax(ruby). Some code

getting from line 100 to 200 of file.rb syntax(ruby 100,200). file.rb

getting block ‘sample’ from file.rb syntax(ruby#sample). file.rb

to create a block: #begin: sample

some code

#end: sample



46
47
48
49
50
51
52
53
54
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
# File 'lib/bookmaker/blackcloth.rb', line 46

def textile_syntax(tag, attrs, cite, content)
  # get syntax
  m, syntax = *attrs.match(/class="(.*?)([# ].*?)?"/)
  syntax = 'plain_text' if tag == "pre"
  
  # set source
  source_file = content
  
  # get block name
  m, block_name = *attrs.match(/id="(.*?)"/ms)
  
  # get line interval
  m, from_line, to_line = *attrs.match(/class=".*? ([0-9]+),([0-9]+)"/)
  
  # code = Bookmaker::Markup.syntax({
  #   :code => content,
  #   :syntax => syntax,
  #   :source_file => source_file,
  #   :block_name => block_name,
  #   :from_line => from_line,
  #   :to_line => to_line
  # })
  
  content = Bookmaker::Markup.content_for({
    :code => content,
    :syntax => syntax,
    :source_file => source_file,
    :block_name => block_name,
    :from_line => from_line,
    :to_line => to_line
  })
  @@syntax_blocks << [syntax, content]
  position = @@syntax_blocks.size - 1
  %(@syntax:#{position})
end

#to_html(*rules) ⇒ Object

overriding to_html method



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/bookmaker/blackcloth.rb', line 116

def to_html( *rules )
    rules = DEFAULT_RULES if rules.empty?
    # make our working copy
    text = self.dup
    
    @urlrefs = {}
    @shelf = []
    textile_rules = [:refs_textile, :block_textile_table, :block_textile_lists,
                     :block_textile_prefix, :inline_textile_image, :inline_textile_link,
                     :inline_textile_code, :inline_textile_span, :glyphs_textile]
    markdown_rules = [:refs_markdown, :block_markdown_setext, :block_markdown_atx, :block_markdown_rule,
                      :block_markdown_bq, :block_markdown_lists, 
                      :inline_markdown_reflink, :inline_markdown_link]
    @rules = rules.collect do |rule|
        case rule
        when :markdown
            markdown_rules
        when :textile
            textile_rules
        else
            rule
        end
    end.flatten

    # standard clean up
    incoming_entities text 
    clean_white_space text 

    # start processor
    @pre_list = []
    rip_offtags text
    no_textile text
    hard_break text 
    unless @lite_mode
        refs text
        blocks text
    end
    inline text
    smooth_offtags text

    retrieve text

    text.gsub!( /<\/?notextile>/, '' )
    text.gsub!( /x%x%/, '&#38;' )
    clean_html text if filter_html
    text.strip!
    text

end