Class: ClothMark

Inherits:
Object
  • Object
show all
Defined in:
lib/clothmark.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, markup = "markdown", additional_html = false, output = nil) ⇒ ClothMark

ClothMark will generate a default filename if user don’t want to save output to a specific file and will use Markdown as a default markup language.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/clothmark.rb', line 12

def initialize(file, markup = "markdown", additional_html = false, output = nil)
  @file = file

  # ClothMark will raise an error if unknown markup language was sent
  if %w(markdown textile bbcode).include? markup
    @markup = markup
  else
    raise ArgumentError, "Unknown markup language (#{markup})"
  end

  if (!output || output.empty?)
    @output = "#{file.gsub(/(\.[a-z]{3,4})/, '')}_clothmark.html"
  else
    @output = output
  end
  @data_for_output = []
  @additional_html = additional_html # If true, then ClothMark will generate additional CSS and HTML
end

Instance Attribute Details

#data_for_outputObject

Returns the value of attribute data_for_output.



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

def data_for_output
  @data_for_output
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#markupObject (readonly)

Returns the value of attribute markup.



8
9
10
# File 'lib/clothmark.rb', line 8

def markup
  @markup
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#read_from_fileObject

Read lines from input file and put them to array



32
33
34
35
36
37
38
39
40
41
# File 'lib/clothmark.rb', line 32

def read_from_file
  begin
  File.open(@file) do |file|
    file.each_line {|line| @data_for_output << line}
  end
  rescue Errno::ENOENT => e
    puts e
    exit
  end
end

#save_to_fileObject

Save output to the output file (one paragraph per line). Include additional HTML and CSS if attr additional_html = true



57
58
59
60
61
62
63
# File 'lib/clothmark.rb', line 57

def save_to_file
  File.open(@output, 'w+') do |file|
    file.puts HEADER if @additional_html
    file.puts @data_for_output.join("\n")
    file.puts FOOTER if @additional_html
  end
end

#to_htmlObject

Convert input to HTML with predefined markup language.



44
45
46
47
48
49
50
51
52
53
# File 'lib/clothmark.rb', line 44

def to_html
  case @markup
  when 'markdown'
    @data_for_output.collect! {|line| BlueCloth.new(line).to_html}
  when 'textile'
    @data_for_output.collect! {|line| RedCloth.new(line).to_html}
  when 'bbcode'
    @data_for_output.collect! {|line| line.bbcode_to_html}
  end
end