Class: ZineBrewer::Application

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

Constant Summary collapse

Commons =
"[hlink]: common/hlink.svg"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Application

Returns a new instance of Application.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zine_brewer/main.rb', line 17

def initialize(path)

  begin
    Encoding.default_external =  Kconv.guess(File.open(path, 'r:BINARY').read)
    input_data = File.open(path, 'rt').read.encode('UTF-8')
  rescue
    raise 'ERROR: The input file does not exist. Check it.'
  end

  @dir = File.dirname(path)

  header, body = /\A((?:.|\n)*?)<%-- page -->/.match(input_data).yield_self do |m|
    if m.nil?
      ['', "\n\n" + Commons + "\n\n" + input_data]
    else
      [m[1], '<%-- page -->' + "\n\n" + Commons + "\n\n" + m.post_match]
    end
  end

  @article_id = if header.sub!(/^■記事ID■[^0-9]+(\d+)$/, '')
                  Regexp.last_match[1]
                else
                  /^\d+/.match(File.basename(@dir)).yield_self{|m| m.nil? ? '■記事ID■' : m[0]}
                end
  h = header.strip.split(/\n\n+/)
  @corner, @title, @lead, @author = [0, 1, 2, 4].map{|i| set_header_item(h[i], '')}
  @pic = set_header_item(h[3], ''){ "#{@article_id}_#{h[3]}" }
  @css = set_header_item(h[5], ''){ h[5].each_line.map{|i| '.c-article_content ' + i }.join }

  @pp_header = make_pp_header
  @converted = convert(body)
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def author
  @author
end

#convertedObject (readonly)

Returns the value of attribute converted.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def converted
  @converted
end

#cornerObject (readonly)

Returns the value of attribute corner.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def corner
  @corner
end

#cssObject (readonly)

Returns the value of attribute css.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def css
  @css
end

#leadObject (readonly)

Returns the value of attribute lead.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def lead
  @lead
end

#picObject (readonly)

Returns the value of attribute pic.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def pic
  @pic
end

#titleObject (readonly)

Returns the value of attribute title.



15
16
17
# File 'lib/zine_brewer/main.rb', line 15

def title
  @title
end

Instance Method Details

#convert(body) ⇒ Object

Converts markdown to html and returns body



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/zine_brewer/main.rb', line 100

def convert(body)
  dkmn = Darkmouun.document.new(body, {:auto_ids => false, :input => 'sekd'}, :se_html)

  ### Sets templates
  dkmn.add_templates "#{__dir__}/templates/", *Dir['*.rb',base:"#{__dir__}/templates/"]

  ### Sets pre process
  dkmn.pre_process = lambda do |t|
  end

  ### Sets post process
  dkmn.post_process = lambda do |t|
    t.gsub!('(((BR)))', '<br/>')
    t.gsub!(/■記事ID■/, @article_id)
    t.gsub!(//, "")
    t.gsub!(/[‘’]/, "'")
    t.gsub!(/<li>\\/, '<li>')
    t << "</div>" if /<div id="p1">/ =~ t
  end

  ### Converts a markdown document and returns that converted body
  dkmn.convert
end

#make_pp_headerObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/zine_brewer/main.rb', line 61

def make_pp_header
  header_output = []
  header_output << "[コーナー]\n#{@corner}" if @corner.is_complete?
  header_output << "[タイトル]\n#{@title}" if @title.is_complete?
  header_output << "[リード]\n<p>#{@lead}</p>" if @lead.is_complete?
  header_output << "[タイトル画像]\n#{@pic}" if @pic.is_complete?
  header_output << "[著者クレジット]\n#{@author}" if @author.is_complete?
  header_output << "[追加CSS]\n#{@css}" if @css.is_complete?
  header_output.join("\n\n")
end

#make_proof_directoryObject



79
80
81
82
83
84
85
# File 'lib/zine_brewer/main.rb', line 79

def make_proof_directory
  @proof_dir = @dir + '/proof'
  begin
    Dir.mkdir(@proof_dir)
  rescue Errno::EEXIST
  end
end

#pretty_printObject

pretty print of the converted document



125
126
127
# File 'lib/zine_brewer/main.rb', line 125

def pretty_print
  @pp_header + "\n\n" + @converted
end

#set_header_item(value, alt) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/zine_brewer/main.rb', line 50

def set_header_item(value, alt)
  if /\A\ufeff?[\-%]+\Z/ =~ value || value.nil?
    alt.define_singleton_method(:is_complete?){ false }
    alt
  else
    value = yield if block_given?
    value.define_singleton_method(:is_complete?){ true }
    value
  end
end

#write_outObject

Writing out header and body to each file



73
74
75
76
77
# File 'lib/zine_brewer/main.rb', line 73

def write_out
  make_proof_directory
  write_proof_header
  write_proof_body
end

#write_proof_bodyObject



93
94
95
96
97
# File 'lib/zine_brewer/main.rb', line 93

def write_proof_body
  File.open("#{@proof_dir}/body.txt", 'wb') do |f|
    f.write(@converted.gsub("<!-- page_delimiter -->\n", ''))
  end
end

#write_proof_headerObject



87
88
89
90
91
# File 'lib/zine_brewer/main.rb', line 87

def write_proof_header
  File.open("#{@proof_dir}/header.txt", 'wb') do |f|
    f.write(@pp_header)
  end
end