Class: Jekyll::PandocFile

Inherits:
Object
  • Object
show all
Includes:
Convertible
Defined in:
lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, format, posts, title = nil) ⇒ PandocFile

Returns a new instance of PandocFile.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 32

def initialize(site, format, posts, title = nil)
  @site   = site
  @config = JekyllPandocMultipleFormats::Config.new(@site.config['pandoc']).config
  @format = format
  @flags  = []

  if posts.is_a? Array
    @posts = posts

    raise ArgumentError.new "'title' argument is required for multipost file" unless title

    @title = title
  else
    @posts = [posts]
    @title = posts.data['title'] unless title
  end

  @slug  = Utils.slugify(title)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def config
  @config
end

#flagsObject (readonly)

Returns the value of attribute flags.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def flags
  @flags
end

#formatObject (readonly)

Returns the value of attribute format.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def format
  @format
end

#papersizeObject (readonly)

Returns the value of attribute papersize.



30
31
32
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 30

def papersize
  @papersize
end

#postsObject (readonly)

Returns the value of attribute posts.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def posts
  @posts
end

#sheetsizeObject (readonly)

Returns the value of attribute sheetsize.



30
31
32
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 30

def sheetsize
  @sheetsize
end

#signatureObject (readonly)

Returns the value of attribute signature.



30
31
32
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 30

def signature
  @signature
end

#siteObject (readonly)

Returns the value of attribute site.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def site
  @site
end

#slugObject (readonly)

Returns the value of attribute slug.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title.



29
30
31
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 29

def title
  @title
end

#urlObject (readonly)

if it’s just one article, the url is the same except it’s not html otherwise we use the permalink template with some placeholder



74
75
76
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 74

def url
  @url
end

Instance Method Details

#binary?Boolean

These formats are binary files and must use the -o flag

Returns:

  • (Boolean)


234
235
236
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 234

def binary?
  %w[pdf epub epub3 odt docx].include? @format
end

#commandObject



221
222
223
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 221

def command
  'pandoc ' << flags
end

#contentObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 132

def content
  if single_post?
    single_post.content
  else
    header_re = /^(#+.*\n*|.*\n[=-]+\n*)\Z/
    bib_title = ""
    @posts.map do |post|
      bib_title = post.content.match(header_re).to_s if bib_title.empty?
      # remove bibliography titles
      # since pandoc does it's own bibliography output, it recommends
      # leaving an empty chapter title to mark it as such
      post.content.gsub(header_re, '')
    # we add the first bibliography title we can find in the end
    end.join("\n\n\n") << bib_title
  end
end

#coverObject

Returns a cover, without checking if it exists

It assumes covers are in PNG format



173
174
175
176
177
178
179
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 173

def cover
  if single_post? && single_post.data['cover']
    File.join(@site.config['source'], single_post.data['cover'])
  else
    File.join(@site.config['source'], @config['covers_dir'], "#{@slug}.png")
  end
end

#epub?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 229

def epub?
  %w[epub epub3].include? @format
end

#has_cover?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 242

def has_cover?
  File.exists? cover
end

#pathObject



52
53
54
55
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 52

def path
  # path is full destination path with all elements of permalink
  path = @site.in_dest_dir(relative_path)
end

#pdf?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 225

def pdf?
  @format == 'pdf'
end

#pdf_coverObject

Returns a PDF cover



182
183
184
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 182

def pdf_cover
  cover.gsub(/\.[^\.]+\Z/, '.pdf')
end

#pdf_cover!Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 186

def pdf_cover!
  if has_cover? && !File.exists?(pdf_cover)
    Open3::popen3("convert \"#{cover}\" \"#{pdf_cover}\"") do |stdin, stdout, stderr, thread|
      STDERR.print stderr.read

      # Wait for the process to finish
      thread.value
    end
  end

  File.exists?(pdf_cover)
end

#relative_pathObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 57

def relative_path
  path = URL.unescape_path(url)
  path.gsub! /^\//, ''

  # but if the post is going to be index.html, use slug + format
  # (ie /year/month/slug/slug.pdf)
  if url.end_with? '/'
    path = File.join(path, @slug)
    path << '.'
    path << @format
  end

  path
end

#single_post?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 238

def single_post?
  @posts.count == 1
end

#url_placeholdersObject



85
86
87
88
89
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 85

def url_placeholders
  { output_ext: @format,
    slug: @slug,
    title: @title }
end

#writeObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 149

def write
  FileUtils.mkdir_p(File.dirname(path))
  # Remove the file before creating it
  FileUtils.rm_f(path)
  # Move to the source dir since everything will be relative to that
  Dir::chdir(@site.config['source']) do
    # Do the stuff
    Open3::popen3(command) do |stdin, stdout, stderr, thread|
      stdin.puts 
      stdin.puts content
      stdin.close
      STDERR.print stderr.read

      # Wait for the process to finish
      thread.value
    end
  end

  File.exists?(path)
end

#yaml_metadataObject

adds post metadata as yaml metadata



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
129
130
# File 'lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb', line 92

def 
  if single_post?

    # if we were to merge config to data, the default options would
    # take precedence
    data = Jekyll::Utils.deep_merge_hashes @config, single_post.data
    single_post.merge_data! data


    # we extract the excerpt because it serializes as an object and
    # breaks pandoc
     = single_post.data.reject{ |k| k == 'excerpt' }

    if @config['date_format']
      ['date'] = ['date'].strftime(@config['date_format'])
    else
      .delete('date')
    end
  else
    # we have to use this fugly syntax because jekyll doesn't do
    # symbols
     = {
      'date'      => @config['date_format'] ? Date.today.strftime(@config['date_format']) : nil,
      'title'     => @title,
      'author'    => nil,
      'papersize' => papersize,
      'sheetsize' => sheetsize,
      'signature' => signature
    }
  end

  # fix page sizes, pandoc uses 'A4' while printer.rb uses
  # 'a4paper'
  %w[papersize sheetsize].each do |size|
    [size] = fix_size [size]
  end

  .to_yaml << "\n---\n"
end