Module: Jekyll::PandocExports
- Defined in:
- lib/jekyll-pandoc-exports/hooks.rb,
lib/jekyll-pandoc-exports/command.rb,
lib/jekyll-pandoc-exports/version.rb,
lib/jekyll-pandoc-exports/generator.rb,
lib/jekyll-pandoc-exports/statistics.rb
Defined Under Namespace
Classes: Command, ExportRunner, Hooks, Statistics
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
- .apply_template(html_content, config) ⇒ Object
- .build_download_html(generated_files, config) ⇒ Object
- .build_download_url(site, config, filename, extension) ⇒ Object
- .clean_unicode_characters(html) ⇒ Object
- .generate_docx(html_content, filename, output_dir, site, generated_files, config = {}) ⇒ 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
- .log_error(config, message) ⇒ Object
- .log_message(config, message) ⇒ 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_content_size(html_content, config) ⇒ Object
- .validate_dependencies ⇒ Object
Class Method Details
.apply_template(html_content, config) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 195 def self.apply_template(html_content, config) template = config['template'] || {} header = template['header'].to_s = template['footer'].to_s css = template['css'].to_s return html_content if header.empty? && .empty? && css.empty? # Add custom CSS unless css.empty? css_tag = "<style>#{css}</style>" html_content = html_content.sub(/<\/head>/, "#{css_tag}\n</head>") end # Add header after body tag unless header.empty? html_content = html_content.sub(/<body[^>]*>/, "\&\n#{header}") end # Add footer before closing body tag unless .empty? html_content = html_content.sub(/<\/body>/, "#{}\n</body>") end html_content end |
.build_download_html(generated_files, config) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 309 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 |
.build_download_url(site, config, filename, extension) ⇒ Object
329 330 331 332 333 334 335 336 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 329 def self.build_download_url(site, config, filename, extension) output_dir = config['output_dir'] || '' if output_dir.empty? "#{site.baseurl}/#{filename}.#{extension}" else "#{site.baseurl}/#{output_dir}/#{filename}.#{extension}" end end |
.clean_unicode_characters(html) ⇒ Object
290 291 292 293 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 290 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, config = {}) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 221 def self.generate_docx(html_content, filename, output_dir, site, generated_files, config = {}) return unless validate_content_size(html_content, config) begin # Run pre-conversion hooks processed_html = Hooks.run_pre_conversion_hooks(html_content, config, { format: :docx, filename: filename }) docx_content = PandocRuby.convert(processed_html, from: :html, to: :docx) # Run post-conversion hooks docx_content = Hooks.run_post_conversion_hooks(docx_content, :docx, config, { filename: filename }) 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: build_download_url(site, config, filename, 'docx') } @stats&.record_conversion_success(:docx) (config, "Generated #{filename}.docx") rescue => e @stats&.record_conversion_failure(:docx, e) log_error(config, "Failed to generate #{filename}.docx: #{e.}") end end |
.generate_pdf(html_content, filename, output_dir, site, generated_files, page, config) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 248 def self.generate_pdf(html_content, filename, output_dir, site, generated_files, page, config) return unless validate_content_size(html_content, config) begin # Run pre-conversion hooks processed_html = Hooks.run_pre_conversion_hooks(html_content, config, { format: :pdf, filename: filename }) pdf_html = processed_html.dup # Apply Unicode cleanup if enabled if config['unicode_cleanup'] pdf_html = clean_unicode_characters(pdf_html) end # Apply title cleanup patterns from config # (Note: also applied in process_html_content for both formats) # Get PDF options from config or page front matter = page.data['pdf_options'] || config['pdf_options'] # Merge custom pandoc options = .merge(config['pandoc_options']) pdf_content = PandocRuby.new(pdf_html, from: :html, to: :pdf).convert() # Run post-conversion hooks pdf_content = Hooks.run_post_conversion_hooks(pdf_content, :pdf, config, { filename: filename }) 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: build_download_url(site, config, filename, 'pdf') } @stats&.record_conversion_success(:pdf) (config, "Generated #{filename}.pdf") rescue => e @stats&.record_conversion_failure(:pdf, e) log_error(config, "Failed to generate #{filename}.pdf: #{e.}") end end |
.get_html_file_path(site, page) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 167 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
134 135 136 137 138 139 140 141 142 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 134 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
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 121 def self.get_output_filename(item) # Support custom export filenames via front matter if item.respond_to?(:data) && item.data['export_filename'] return item.data['export_filename'] end 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
295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 295 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) html_content end |
.log_error(config, message) ⇒ Object
338 339 340 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 338 def self.log_error(config, ) Jekyll.logger.error "Pandoc Exports:", end |
.log_message(config, message) ⇒ Object
321 322 323 324 325 326 327 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 321 def self.(config, ) if config['debug'] Jekyll.logger.info "Pandoc Exports [DEBUG]:", else Jekyll.logger.info "Pandoc Exports:", end end |
.process_collections(site, config) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 63 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 176 def self.process_html_content(html_content, site, config) processed = html_content.dup # Apply template customizations processed = apply_template(processed, config) # Apply HTML cleanup patterns (removes unwanted elements before conversion) (config['title_cleanup'] || []).each do |pattern| processed.gsub!(Regexp.new(pattern, Regexp::MULTILINE), '') end # 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
77 78 79 80 81 82 83 84 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 77 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 144 def self.process_page(site, page, config) @stats&.record_processing_start @stats&.record_file_processed 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, config) 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 @stats&.record_processing_end end |
.setup_configuration(site) ⇒ Object
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 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 22 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' => [], 'debug' => false, 'max_file_size' => 10_000_000, 'strict_size_limit' => false, 'performance_monitoring' => false, 'template' => { 'header' => '', 'footer' => '', 'css' => '' }, 'pandoc_options' => {} }.merge(config) end |
.skip_unchanged_file?(site, item, config) ⇒ Boolean
86 87 88 89 90 91 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 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 86 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) # Check if any required output files are missing if item.data['docx'] && !File.exist?(docx_file) return false end if item.data['pdf'] && !File.exist?(pdf_file) return false end # Check if any existing output files are older than source 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_content_size(html_content, config) ⇒ Object
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 342 def self.validate_content_size(html_content, config) max_size = config['max_file_size'] || 10_000_000 # 10MB default if html_content.bytesize > max_size Jekyll.logger.warn "Pandoc Exports:", "Content size (#{html_content.bytesize} bytes) exceeds recommended limit (#{max_size} bytes)" return false if config['strict_size_limit'] end true end |
.validate_dependencies ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jekyll-pandoc-exports/generator.rb', line 48 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 |