Skip to content

Commit

Permalink
feat(pdf): support rendering into result string
Browse files Browse the repository at this point in the history
BREAKING:

- style_filename - markdown parameters switched
as-op committed Nov 1, 2022
1 parent a65a978 commit bfc4669
Showing 8 changed files with 69 additions and 24 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## [Unreleased]

## [0.0.5] - 2022-11-01

- feat(pdf): support rendering into result string

BREAKING:

- style_filename - markdown parameters switched

## [0.0.4] - 2022-11-01

- feat(blockcode): support background color and border
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
md_to_pdf (0.0.4)
md_to_pdf (0.0.5)
commonmarker (~> 0.23.6)
front_matter_parser (~> 1.0)
matrix (~> 0.4.2)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -34,13 +34,13 @@ require 'md_to_pdf'
styling_filename = './demo/demo.yml'
source_filename = './demo/demo.md'
dest_filename = './demo/generated/demo.pdf'
MarkdownToPDF.generate_markdown_pdf(styling_filename, source_filename, dest_filename)
MarkdownToPDF.generate_markdown_pdf(source_filename, styling_filename, dest_filename)

# example with raw markdown pre-processing
markdown_string = File.read(source_filename)
markdown_string += 'An additional text at the bottom'
images_path = File.dirname(source_filename)
MarkdownToPDF.generate_markdown_string_pdf(styling_filename, markdown_string, images_path, dest_filename)
MarkdownToPDF.generate_markdown_string_pdf(markdown_string, styling_filename, images_path, dest_filename)
```

## Markdown
6 changes: 3 additions & 3 deletions bin/md_to_pdf
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ if ARGV.length < 3
exit
end

styling_filename = File.expand_path(ARGV[0])
source_filename = File.expand_path(ARGV[1])
source_filename = File.expand_path(ARGV[0])
styling_filename = File.expand_path(ARGV[1])
dest_filename = File.expand_path(ARGV[2])

unless File.file?(source_filename)
@@ -24,6 +24,6 @@ puts "Styling: #{styling_filename}"
puts "Source: #{source_filename}"
puts "Destination: #{dest_filename}"

MarkdownToPDF.generate_markdown_pdf(styling_filename, source_filename, dest_filename)
MarkdownToPDF.generate_markdown_pdf(source_filename, styling_filename, dest_filename)

puts 'Done.'
48 changes: 36 additions & 12 deletions lib/md_to_pdf.rb
Original file line number Diff line number Diff line change
@@ -23,22 +23,36 @@ def initialize(fonts_path:, styling_image_path:, styling_yml_filename:)
end

def file_to_pdf(markdown_file, pdf_destination, images_path = nil)
FileUtils.mkdir_p File.dirname(pdf_destination)
markdown = File.read(markdown_file)
markdown_to_pdf(markdown, pdf_destination, images_path || File.dirname(markdown_file))
end

def file_to_pdf_bin(markdown_file, images_path = nil)
markdown = File.read(markdown_file)
markdown_to_pdf_bin(markdown, images_path || File.dirname(markdown_file))
end

def markdown_to_pdf(markdown_string, pdf_destination, images_path)
FileUtils.mkdir_p File.dirname(pdf_destination)
render_markdown(markdown_string, images_path)
@pdf.render_file(pdf_destination)
end

def markdown_to_pdf_bin(markdown_string, images_path)
render_markdown(markdown_string, images_path)
@pdf.render
end

private

def render_markdown(markdown_string, images_path)
pdf_setup_document
@images_path = images_path
doc = Parser.new.parse_markdown(markdown_string, @styles.default_fields)
@hyphens = Hyphenate.new(doc[:language], doc[:hyphenation])
render_doc(doc)
@pdf.render_file(pdf_destination)
end

private

def render_doc(doc)
opts = @convert.pdf_root_options(@styles.page)
root = doc[:root]
@@ -909,21 +923,31 @@ def with_block_margin_all(opts, &block)
end
end

def self.generate_markdown_pdf(styling_yml_filename, markdown_file, destination_filename)
renderer = MarkdownToPDF::Generator.new(
def self.init_generator(styling_yml_filename)
MarkdownToPDF::Generator.new(
styling_yml_filename: styling_yml_filename,
styling_image_path: File.join(File.dirname(styling_yml_filename), 'images'),
fonts_path: File.join(File.dirname(styling_yml_filename), 'fonts')
)
end

def self.generate_markdown_pdf(markdown_file, styling_yml_filename, destination_filename)
renderer = init_generator(styling_yml_filename)
renderer.file_to_pdf(markdown_file, destination_filename)
end

def self.generate_markdown_string_pdf(styling_yml_filename, markdown_string, images_path, destination_filename)
renderer = MarkdownToPDF::Generator.new(
styling_yml_filename: styling_yml_filename,
styling_image_path: File.join(File.dirname(styling_yml_filename), 'images'),
fonts_path: File.join(File.dirname(styling_yml_filename), 'fonts')
)
def self.render_markdown(markdown_file, styling_yml_filename)
renderer = init_generator(styling_yml_filename)
renderer.file_to_pdf_bin(markdown_file)
end

def self.generate_markdown_string_pdf(markdown_string, styling_yml_filename, images_path, destination_filename)
renderer = init_generator(styling_yml_filename)
renderer.markdown_to_pdf(markdown_string, destination_filename, images_path)
end

def self.render_markdown_string(markdown_string, styling_yml_filename, images_path)
renderer = init_generator(styling_yml_filename)
renderer.markdown_to_pdf_bin(markdown_string, images_path)
end
end
2 changes: 1 addition & 1 deletion lib/md_to_pdf/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MarkdownToPDF
VERSION = '0.0.4'
VERSION = '0.0.5'
end
12 changes: 10 additions & 2 deletions sig/md_to_pdf.rbs
Original file line number Diff line number Diff line change
@@ -8,10 +8,18 @@ module MarkdownPDF

def file_to_pdf: (markdown_file: String, pdf_destination_file: String, images_path: String | nil) -> void

def file_to_pdf_bin: (markdown_file: String, images_path: String | nil) -> String

def markdown_to_pdf: (markdown_string: String, pdf_destination_file: String, images_path: String | nil) -> void

def markdown_to_pdf_bin: (markdown_string: String, images_path: String | nil) -> String
end

def generate_markdown_pdf: (styling_yml_filename: String, markdown_file: String, pdf_destination_file: String) -> void
def render_markdown: (markdown_file: String, styling_yml_filename: String, images_path: String) -> String

def render_markdown_string: (markdown_string: String, styling_yml_filename: String, images_path: String) -> String

def generate_markdown_pdf: (markdown_file: String, styling_yml_filename: String, pdf_destination_file: String) -> void

def generate_markdown_string_pdf: (styling_yml_filename: String, markdown_string: String, images_path: String, destination_filename: String) -> void
def generate_markdown_string_pdf: (markdown_string: String, styling_yml_filename: String, images_path: String, pdf_destination_file: String) -> void
end
11 changes: 8 additions & 3 deletions spec/markdown_to_pdf/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -10,15 +10,20 @@
dest_filename = File.join(demo_dir, 'generated', 'demo.pdf')

describe MarkdownToPDF::Generator do
it "generate the demo via module" do
it "generate the demo file via module" do
File.delete(dest_filename) if File.exist?(dest_filename)
MarkdownToPDF.generate_markdown_pdf(styling_filename, source_filename, dest_filename)
MarkdownToPDF.generate_markdown_pdf(source_filename, styling_filename, dest_filename)
expect(File.exist?(dest_filename)).to be(true)
end

it "generate the demo binary via module" do
content = MarkdownToPDF.render_markdown(source_filename, styling_filename)
expect(content).to start_with "%PDF-1.4\n"
end

it "generate the demo via cmdline" do
File.delete(dest_filename) if File.exist?(dest_filename)
system("bundle exec md_to_pdf #{styling_filename} #{source_filename} #{dest_filename} >/dev/null 2>&1")
system("bundle exec md_to_pdf #{source_filename} #{styling_filename} #{dest_filename} >/dev/null 2>&1")
expect(File.exist?(dest_filename)).to be(true)
end
end

0 comments on commit bfc4669

Please sign in to comment.