Class: BlackCloth

Inherits:
RedCloth show all
Defined in:
lib/kitabu/blackcloth.rb

Constant Summary collapse

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

Constants inherited from RedCloth

RedCloth::DEFAULT_RULES, RedCloth::VERSION

Instance Attribute Summary

Attributes inherited from RedCloth

#filter_html, #filter_styles, #hard_breaks, #lite_mode, #no_span_caps, #rules

Instance Method Summary collapse

Methods inherited from RedCloth

#initialize

Constructor Details

This class inherits a constructor from RedCloth

Instance Method Details

#image_size(img) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/kitabu/blackcloth.rb', line 115

def image_size(img)
  puts File.expand_path(img)
  output = IO.popen("file images/#{img}").read
  m, width, height = *output.match(/([0-9]+) x ([0-9]+)/)
  
  [width.to_i, height.to_i]
end

#inline(text) ⇒ Object

overriding inline method



124
125
126
127
# File 'lib/kitabu/blackcloth.rb', line 124

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/kitabu/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/kitabu/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



91
92
93
# File 'lib/kitabu/blackcloth.rb', line 91

def syntax_blocks
  @@syntax_blocks
end

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

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



107
108
109
110
111
112
113
# File 'lib/kitabu/blackcloth.rb', line 107

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_file(tag, attrs, cite, content) ⇒ Object

Usage: file. app/models/users.rb

Remember to set ‘base_url` in your Kitabu configuration file



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kitabu/blackcloth.rb', line 37

def textile_file(tag, attrs, cite, content)
  base_url = Kitabu::Base.config["base_url"]
  
  if base_url
    url = File.join(base_url, content)
  else
    url = content
    $stdout << "\nYou're using `file. #{content}` but didn't set base_url in your configuration file.\n"
  end
  
  %(<p class="file"><span><strong>Download</strong> <a href="#{url}">#{content}</a></span></p>)
end

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

Usage: note. Some text



102
103
104
# File 'lib/kitabu/blackcloth.rb', line 102

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

#textile_pre(*args) ⇒ Object

Usage: pre. Some code



96
97
98
99
# File 'lib/kitabu/blackcloth.rb', line 96

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



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
# File 'lib/kitabu/blackcloth.rb', line 63

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]+)"/)
  
  content = Kitabu::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



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kitabu/blackcloth.rb', line 130

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%/sm, '&#38;' )
    clean_html text if filter_html
    text.strip!
    text

end