-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rb
83 lines (78 loc) · 2.21 KB
/
build.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'prawn'
require 'prawn-svg'
require 'prawn/measurement_extensions'
require 'front_matter_parser'
# require './fix.rb'
serif = 'fonts/Signifier-Regular.otf'
sans = 'fonts/Söhne-Buch.otf'
serif = 'fonts/Lora-Regular.ttf'
sans = 'fonts/Roboto-Light.ttf'
puts 'Generating documents'
puts
def extract_hierarchy(text, separator)
chapters = []
paragraph = ''
text.each_line do |line|
if line.start_with? separator
chapters << paragraph unless paragraph.empty?
paragraph = line.gsub(separator, '')
else
paragraph += "#{line}\n"
end
end
chapters << paragraph
chapters
end
Dir.glob('data/*') do |file|
data = FrontMatterParser::Parser.parse_file(file)
title = data.front_matter['title']
version = data.front_matter['version']
document_name = "#{ file.split('/').last.gsub('.md', '') }-#{ version }.pdf"
file_name = "pdfs/#{ document_name }"
puts title
Prawn::Document.generate( file_name,
page_size: 'A4',
top_margin: 10.mm,
right_margin: 10.mm,
bottom_margin: 10.mm,
left_margin: 65.mm) do
chapters = extract_hierarchy data.content, '# '
chapters.each_with_index do |chapter, index|
parts = chapter.split("\n")
h2 = "#{index+1}. #{parts.shift}"
p = parts.join("\n")
puts h2
move_down 5.mm unless index.zero?
font sans
font_size 12
text h2
move_down 2.mm
font serif
font_size 9
text p, leading: 1
end
# On each page
repeat(:all, dynamic: true) do
font sans
font_size 22
text_box title,
at: [-55.mm, 277.mm],
width: 50.mm,
height: 75.mm
line_width 0.25
[
document_name,
data.front_matter['date'],
"Version #{ version }",
"Page #{ page_number }",
''
].reverse.each_with_index do |mention, index|
y = 10.mm + index * 10.mm
stroke_line [-55.mm, y], [-10.mm, y]
draw_text mention, at: [-55.mm, y-6.mm], size: 8
end
svg IO.read("assets/logo-noesya.svg"), at: [-55.mm, 5.mm], width: 22.mm
end
end
puts
end