Module: Jekyll::PandocExports
- Defined in:
- lib/jekyll-pandoc-exports/version.rb,
lib/jekyll-pandoc-exports/generator.rb
Constant Summary collapse
- VERSION =
"0.1.4"
Class Method Summary collapse
- .build_download_html(generated_files, config) ⇒ Object
- .clean_unicode_characters(html) ⇒ Object
- .generate_docx(html_content, filename, output_dir, site, generated_files) ⇒ Object
- .generate_pdf(html_content, filename, output_dir, site, generated_files, page, config) ⇒ Object
- .get_html_file_path(site, page) ⇒ Object
- .get_output_directory(site, config) ⇒ Object
- .get_output_filename(item) ⇒ Object
- .inject_download_links(html_content, generated_files, html_file, config) ⇒ Object
- .process_collections(site, config) ⇒ Object
- .process_html_content(html_content, site, config) ⇒ Object
- .process_item(site, item, config) ⇒ Object
- .process_page(site, page, config) ⇒ Object
- .setup_configuration(site) ⇒ Object
- .skip_unchanged_file?(site, item, config) ⇒ Boolean
- .validate_dependencies ⇒ Object
Class Method Details
.build_download_html(generated_files, config) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 220 def self.build_download_html(generated_files, config) download_html = "<div class=\"#{config['download_class']}\" style=\"#{config['download_style']}\">" + "<p><strong>Download Options:</strong></p>" + "<ul style=\"margin: 5px 0; padding-left: 20px;\">" generated_files.each do |file| download_html += "<li><a href=\"#{file[:url]}\" style=\"color: #007bff; text-decoration: none; font-weight: bold;\">#{file[:type]}</a></li>" end download_html += "</ul></div>" end |
.clean_unicode_characters(html) ⇒ Object
202 203 204 205 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 202 def self.clean_unicode_characters(html) # Remove emoji and symbol ranges that cause LaTeX issues html.gsub(/[\u{1F000}-\u{1F9FF}]|[\u{2600}-\u{26FF}]|[\u{2700}-\u{27BF}]/, '') end |
.generate_docx(html_content, filename, output_dir, site, generated_files) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 153 def self.generate_docx(html_content, filename, output_dir, site, generated_files) begin docx_content = PandocRuby.convert(html_content, from: :html, to: :docx) docx_file = File.join(output_dir, "#{filename}.docx") File.open(docx_file, 'wb') { |file| file.write(docx_content) } generated_files << { type: 'Word Document (.docx)', url: "#{site.baseurl}/#{filename}.docx" } Jekyll.logger.info "Pandoc Exports:", "Generated #{filename}.docx" rescue => e Jekyll.logger.error "Pandoc Exports:", "Failed to generate #{filename}.docx: #{e.}" end end |
.generate_pdf(html_content, filename, output_dir, site, generated_files, page, config) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 170 def self.generate_pdf(html_content, filename, output_dir, site, generated_files, page, config) begin pdf_html = html_content.dup # Apply Unicode cleanup if enabled if config['unicode_cleanup'] pdf_html = clean_unicode_characters(pdf_html) end # Apply title cleanup patterns from config config['title_cleanup'].each do |pattern| pdf_html.gsub!(Regexp.new(pattern), '') end # Get PDF options from config or page front matter = page.data['pdf_options'] || config['pdf_options'] pdf_content = PandocRuby.new(pdf_html, from: :html, to: :pdf).convert() pdf_file = File.join(output_dir, "#{filename}.pdf") File.open(pdf_file, 'wb') { |file| file.write(pdf_content) } generated_files << { type: 'PDF Document (.pdf)', url: "#{site.baseurl}/#{filename}.pdf" } Jekyll.logger.info "Pandoc Exports:", "Generated #{filename}.pdf" rescue => e Jekyll.logger.error "Pandoc Exports:", "Failed to generate #{filename}.pdf: #{e.}" end end |
.get_html_file_path(site, page) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 133 def self.get_html_file_path(site, page) # Handle different Jekyll URL structures if page.url.end_with?('/') File.join(site.dest, page.url, 'index.html') else File.join(site.dest, "#{page.url.gsub('/', '')}.html") end end |
.get_output_directory(site, config) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 105 def self.get_output_directory(site, config) if config['output_dir'].empty? site.dest else output_path = File.join(site.dest, config['output_dir']) FileUtils.mkdir_p(output_path) unless Dir.exist?(output_path) output_path end end |
.get_output_filename(item) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 97 def self.get_output_filename(item) if item.respond_to?(:basename) File.basename(item.basename, '.md') else File.basename(item.path, '.md') end end |
.inject_download_links(html_content, generated_files, html_file, config) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 207 def self.inject_download_links(html_content, generated_files, html_file, config) download_html = build_download_html(generated_files, config) # Insert after first heading or at beginning of body if html_content.match(/<h[1-6][^>]*>/) html_content.sub!(/<\/h[1-6]>/, "\\&\n#{download_html}") else html_content.sub!(/<body[^>]*>/, "\\&\n#{download_html}") end File.write(html_file, html_content) end |
.process_collections(site, config) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 49 def self.process_collections(site, config) config['collections'].each do |collection_name| case collection_name when 'pages' site.pages.each { |item| process_item(site, item, config) } when 'posts' site.posts.docs.each { |item| process_item(site, item, config) } else collection = site.collections[collection_name] collection&.docs&.each { |item| process_item(site, item, config) } end end end |
.process_html_content(html_content, site, config) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 142 def self.process_html_content(html_content, site, config) processed = html_content.dup # Apply image path fixes from config config['image_path_fixes'].each do |fix| processed.gsub!(Regexp.new(fix['pattern']), fix['replacement'].gsub('{{site.dest}}', site.dest)) end processed end |
.process_item(site, item, config) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 63 def self.process_item(site, item, config) return unless item.data['docx'] || item.data['pdf'] # Check if file was modified (incremental build) return if skip_unchanged_file?(site, item, config) process_page(site, item, config) end |
.process_page(site, page, config) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 115 def self.process_page(site, page, config) html_file = get_html_file_path(site, page) return unless File.exist?(html_file) html_content = File.read(html_file) processed_html = process_html_content(html_content, site, config) filename = get_output_filename(page) output_dir = get_output_directory(site, config) generated_files = [] generate_docx(processed_html, filename, output_dir, site, generated_files) if page.data['docx'] generate_pdf(processed_html, filename, output_dir, site, generated_files, page, config) if page.data['pdf'] if config['inject_downloads'] && generated_files.any? inject_download_links(html_content, generated_files, html_file, config) end end |
.setup_configuration(site) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 18 def self.setup_configuration(site) config = site.config['pandoc_exports'] || {} { 'enabled' => true, 'output_dir' => '', 'collections' => ['pages', 'posts'], 'pdf_options' => { 'variable' => 'geometry:margin=1in' }, 'unicode_cleanup' => true, 'inject_downloads' => true, 'download_class' => 'pandoc-downloads no-print', 'download_style' => 'margin: 20px 0; padding: 15px; background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 5px;', 'title_cleanup' => [], 'image_path_fixes' => [] }.merge(config) end |
.skip_unchanged_file?(site, item, config) ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 72 def self.skip_unchanged_file?(site, item, config) return false unless config['incremental'] source_file = item.respond_to?(:path) ? item.path : item.relative_path return false unless File.exist?(source_file) filename = get_output_filename(item) output_dir = get_output_directory(site, config) docx_file = File.join(output_dir, "#{filename}.docx") pdf_file = File.join(output_dir, "#{filename}.pdf") source_mtime = File.mtime(source_file) if item.data['docx'] && File.exist?(docx_file) return false if File.mtime(docx_file) < source_mtime end if item.data['pdf'] && File.exist?(pdf_file) return false if File.mtime(pdf_file) < source_mtime end true end |
.validate_dependencies ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 34 def self.validate_dependencies pandoc_available = system('pandoc --version > /dev/null 2>&1') latex_available = system('pdflatex --version > /dev/null 2>&1') unless pandoc_available Jekyll.logger.warn "Pandoc Exports:", "Pandoc not found. Install with: brew install pandoc (macOS) or apt-get install pandoc (Ubuntu)" end unless latex_available Jekyll.logger.warn "Pandoc Exports:", "LaTeX not found. Install with: brew install --cask mactex (macOS) or apt-get install texlive-latex-base (Ubuntu)" end pandoc_available end |