prawn-manual_builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +6 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/lib/prawn/manual_builder.rb +34 -0
- data/lib/prawn/manual_builder/example.rb +391 -0
- data/lib/prawn/manual_builder/example_file.rb +113 -0
- data/lib/prawn/manual_builder/example_package.rb +54 -0
- data/lib/prawn/manual_builder/example_section.rb +47 -0
- data/lib/prawn/manual_builder/syntax_highlight.rb +56 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c85c4457c9cc7079dbd6d92ea0778aab34009074
|
4
|
+
data.tar.gz: fc8dc917c37b3d59791b9d5112e1f55a23e449f7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 582e12aaa4931c67442fec5b0946e4ea7ce384d84ea64e2477d1fbe10a8fc39637a8303cf1db6dfa8032ac950e180363c693804a2c5d1c5048e91bda90d24bdb
|
7
|
+
data.tar.gz: 7cd0ce8266d4544308cd40bf3f10cd35d2f8ff889bf9b4e1d315ce96aa6116c276b57dc5c4f9c54f6137c9bbbef96d4849e56f2b9c780a2c2ec12b9a900f1c6d
|
data/README.md
ADDED
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "enumerator"
|
2
|
+
require "prawn"
|
3
|
+
|
4
|
+
module Prawn
|
5
|
+
module ManualBuilder
|
6
|
+
def self.manual_dir=(dir)
|
7
|
+
@manual_dir = dir
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.manual_dir
|
11
|
+
@manual_dir || Dir.pwd
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.document_class
|
15
|
+
@document_class || Prawn::Document
|
16
|
+
rescue NameError
|
17
|
+
raise "Prawn::ManualBuilder.document_class was not set. "+
|
18
|
+
"Either assign a custom document class, or make sure to install "+
|
19
|
+
"and require the Prawn gem."
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
DATADIR = File.dirname(__FILE__) + "/../../data"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require_relative "manual_builder/example"
|
28
|
+
require_relative "manual_builder/example_file"
|
29
|
+
require_relative "manual_builder/example_section"
|
30
|
+
require_relative "manual_builder/example_package"
|
31
|
+
require_relative "manual_builder/syntax_highlight"
|
32
|
+
require_relative "manual_builder/example"
|
33
|
+
|
34
|
+
|
@@ -0,0 +1,391 @@
|
|
1
|
+
module Prawn
|
2
|
+
module ManualBuilder
|
3
|
+
# The Prawn::ManualBuilder::Example class holds all the helper methods
|
4
|
+
# used to generate manuals.
|
5
|
+
#
|
6
|
+
# The overall structure is to have single example files grouped by package
|
7
|
+
# folders. Each package has a package builder file (with the same name as the
|
8
|
+
# package folder) that defines the inner structure of subsections and
|
9
|
+
# examples. The manual is then built by loading all the packages and some
|
10
|
+
# standalone pages.
|
11
|
+
#
|
12
|
+
# To see one of the examples check manual/basic_concepts/cursor.rb
|
13
|
+
#
|
14
|
+
# To see one of the package builders check
|
15
|
+
# manual/basic_concepts/basic_concepts.rb
|
16
|
+
#
|
17
|
+
# To see how the manual is built check manual/manual/manual.rb (Yes that's a
|
18
|
+
# whole load of manuals)
|
19
|
+
class Example < Prawn::ManualBuilder.document_class
|
20
|
+
|
21
|
+
# Values used for the manual design:
|
22
|
+
|
23
|
+
# This is the default value for the margin box
|
24
|
+
#
|
25
|
+
BOX_MARGIN = 36
|
26
|
+
|
27
|
+
# Additional indentation to keep the line measure with a reasonable size
|
28
|
+
#
|
29
|
+
INNER_MARGIN = 30
|
30
|
+
|
31
|
+
# Vertical Rhythm settings
|
32
|
+
#
|
33
|
+
RHYTHM = 10
|
34
|
+
LEADING = 2
|
35
|
+
|
36
|
+
# Colors
|
37
|
+
#
|
38
|
+
BLACK = "000000"
|
39
|
+
LIGHT_GRAY = "F2F2F2"
|
40
|
+
GRAY = "DDDDDD"
|
41
|
+
DARK_GRAY = "333333"
|
42
|
+
BROWN = "A4441C"
|
43
|
+
ORANGE = "F28157"
|
44
|
+
LIGHT_GOLD = "FBFBBE"
|
45
|
+
DARK_GOLD = "EBE389"
|
46
|
+
BLUE = "0000D0"
|
47
|
+
|
48
|
+
# Used to generate the url for the example files
|
49
|
+
#
|
50
|
+
MANUAL_URL = "http://github.com/prawnpdf/prawn/tree/master/manual"
|
51
|
+
|
52
|
+
|
53
|
+
# Loads a package. Used on the manual.
|
54
|
+
#
|
55
|
+
def load_package(package)
|
56
|
+
load_file(package, package)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Loads a page with outline support. Used on the manual.
|
60
|
+
#
|
61
|
+
def load_page(page)
|
62
|
+
load_file("manual", page)
|
63
|
+
|
64
|
+
outline.define do
|
65
|
+
section(page.gsub("_", " ").capitalize, :destination => page_number)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Opens a file in a given package and evals the source
|
70
|
+
#
|
71
|
+
def load_file(package, file)
|
72
|
+
start_new_page
|
73
|
+
example = ExampleFile.new(package, "#{file}.rb")
|
74
|
+
eval example.generate_block_source
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
# Creates a new ExamplePackage object and yields it to a block in order for
|
79
|
+
# it to be populated with examples, sections and some introduction text.
|
80
|
+
# Used on the package files.
|
81
|
+
#
|
82
|
+
def package(package, &block)
|
83
|
+
ep = ExamplePackage.new(package)
|
84
|
+
ep.instance_eval(&block)
|
85
|
+
ep.render(self)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Renders an ExamplePackage cover page.
|
89
|
+
#
|
90
|
+
# Starts a new page and renders the package introduction text.
|
91
|
+
#
|
92
|
+
def render_package_cover(package)
|
93
|
+
header(package.name)
|
94
|
+
instance_eval &(package.intro_block)
|
95
|
+
|
96
|
+
outline.define do
|
97
|
+
section(package.name, :destination => page_number, :closed => true)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Add the ExampleSection to the document outline within the appropriate
|
102
|
+
# package.
|
103
|
+
#
|
104
|
+
def render_section(section)
|
105
|
+
outline.add_subsection_to(section.package_name) do
|
106
|
+
outline.section(section.name, :closed => true)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Renders an ExampleFile.
|
111
|
+
#
|
112
|
+
# Starts a new page and renders an introductory text, the example source and
|
113
|
+
# evaluates the example source inline whenever that is appropriate according
|
114
|
+
# to the ExampleFile directives.
|
115
|
+
#
|
116
|
+
def render_example(example)
|
117
|
+
start_new_page
|
118
|
+
|
119
|
+
outline.add_subsection_to(example.parent_name) do
|
120
|
+
outline.page(:destination => page_number, :title => example.name)
|
121
|
+
end
|
122
|
+
|
123
|
+
example_header(example.parent_folder_name, example.filename)
|
124
|
+
|
125
|
+
prose(example.introduction_text)
|
126
|
+
|
127
|
+
code(example.source)
|
128
|
+
|
129
|
+
if example.eval?
|
130
|
+
eval_code(example.source)
|
131
|
+
else
|
132
|
+
source_link(example)
|
133
|
+
end
|
134
|
+
|
135
|
+
reset_settings
|
136
|
+
end
|
137
|
+
|
138
|
+
# Render the example header. Used on the example pages of the manual
|
139
|
+
#
|
140
|
+
def example_header(package, example)
|
141
|
+
header_box do
|
142
|
+
register_fonts
|
143
|
+
font('DejaVu', :size => 18) do
|
144
|
+
formatted_text([ { :text => package, :color => BROWN },
|
145
|
+
{ :text => "/", :color => BROWN },
|
146
|
+
{ :text => example, :color => ORANGE }
|
147
|
+
], :valign => :center)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Register fonts used on the manual
|
153
|
+
#
|
154
|
+
def register_fonts
|
155
|
+
kai_file = "#{Prawn::ManualBuilder::DATADIR}/fonts/gkai00mp.ttf"
|
156
|
+
font_families["Kai"] = {
|
157
|
+
:normal => { :file => kai_file, :font => "Kai" }
|
158
|
+
}
|
159
|
+
|
160
|
+
dejavu_file = "#{Prawn::ManualBuilder::DATADIR}/fonts/DejaVuSans.ttf"
|
161
|
+
font_families["DejaVu"] = {
|
162
|
+
:normal => { :file => dejavu_file, :font => "DejaVu" }
|
163
|
+
}
|
164
|
+
end
|
165
|
+
|
166
|
+
# Render a block of text after processing code tags and URLs to be used with
|
167
|
+
# the inline_format option.
|
168
|
+
#
|
169
|
+
# Used on the introducory text for example pages of the manual and on
|
170
|
+
# package pages intro
|
171
|
+
#
|
172
|
+
def prose(str)
|
173
|
+
|
174
|
+
# Process the <code> tags
|
175
|
+
str.gsub!(/<code>([^<]+?)<\/code>/,
|
176
|
+
"<font name='Courier'><b>\\1<\/b><\/font>")
|
177
|
+
|
178
|
+
# Process the links
|
179
|
+
str.gsub!(/(https?:\/\/\S+)/,
|
180
|
+
"<color rgb='#{BLUE}'><link href=\"\\1\">\\1</link></color>")
|
181
|
+
|
182
|
+
inner_box do
|
183
|
+
font("Helvetica", :size => 11) do
|
184
|
+
str.split(/\n\n+/).each do |paragraph|
|
185
|
+
|
186
|
+
text(paragraph.gsub(/\s+/," "),
|
187
|
+
:align => :justify,
|
188
|
+
:inline_format => true,
|
189
|
+
:leading => LEADING,
|
190
|
+
:color => DARK_GRAY)
|
191
|
+
|
192
|
+
move_down(RHYTHM)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
move_down(RHYTHM)
|
198
|
+
end
|
199
|
+
|
200
|
+
# Render a code block. Used on the example pages of the manual
|
201
|
+
#
|
202
|
+
def code(str)
|
203
|
+
pre_text = str.gsub(' ', Prawn::Text::NBSP)
|
204
|
+
pre_text = ::CodeRay.scan(pre_text, :ruby).to_prawn
|
205
|
+
|
206
|
+
font('Courier', :size => 9.5) do
|
207
|
+
colored_box(pre_text, :fill_color => DARK_GRAY)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Renders a dashed line and evaluates the code inline
|
212
|
+
#
|
213
|
+
def eval_code(source)
|
214
|
+
move_down(RHYTHM)
|
215
|
+
|
216
|
+
dash(3)
|
217
|
+
stroke_color(BROWN)
|
218
|
+
stroke_horizontal_line(-BOX_MARGIN, bounds.width + BOX_MARGIN)
|
219
|
+
stroke_color(BLACK)
|
220
|
+
undash
|
221
|
+
|
222
|
+
move_down(RHYTHM*3)
|
223
|
+
begin
|
224
|
+
eval(source)
|
225
|
+
rescue => e
|
226
|
+
puts "Error evaluating example: #{e.message}"
|
227
|
+
puts
|
228
|
+
puts "---- Source: ----"
|
229
|
+
puts source
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
# Renders a box with the link for the example file
|
234
|
+
#
|
235
|
+
def source_link(example)
|
236
|
+
url = "#{MANUAL_URL}/#{example.parent_folder_name}/#{example.filename}"
|
237
|
+
|
238
|
+
reason = [{ :text => "This code snippet was not evaluated inline. " +
|
239
|
+
"You may see its output by running the " +
|
240
|
+
"example file located here:\n",
|
241
|
+
:color => DARK_GRAY },
|
242
|
+
|
243
|
+
{ :text => url,
|
244
|
+
:color => BLUE,
|
245
|
+
:link => url}
|
246
|
+
]
|
247
|
+
|
248
|
+
font('Helvetica', :size => 9) do
|
249
|
+
colored_box(reason,
|
250
|
+
:fill_color => LIGHT_GOLD,
|
251
|
+
:stroke_color => DARK_GOLD,
|
252
|
+
:leading => LEADING*3)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
# Render a page header. Used on the manual lone pages and package
|
257
|
+
# introductory pages
|
258
|
+
#
|
259
|
+
def header(str)
|
260
|
+
header_box do
|
261
|
+
register_fonts
|
262
|
+
font('DejaVu', :size => 24) do
|
263
|
+
text(str, :color => BROWN, :valign => :center)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
# Render the arguments as a bulleted list. Used on the manual package
|
269
|
+
# introductory pages
|
270
|
+
#
|
271
|
+
def list(*items)
|
272
|
+
move_up(RHYTHM)
|
273
|
+
|
274
|
+
inner_box do
|
275
|
+
font("Helvetica", :size => 11) do
|
276
|
+
items.each do |li|
|
277
|
+
float { text("•", :color => DARK_GRAY) }
|
278
|
+
indent(RHYTHM) do
|
279
|
+
text(li.gsub(/\s+/," "),
|
280
|
+
:inline_format => true,
|
281
|
+
:color => DARK_GRAY,
|
282
|
+
:leading => LEADING)
|
283
|
+
end
|
284
|
+
|
285
|
+
move_down(RHYTHM)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
# Renders the page-wide headers
|
292
|
+
#
|
293
|
+
def header_box(&block)
|
294
|
+
bounding_box([-bounds.absolute_left, cursor + BOX_MARGIN],
|
295
|
+
:width => bounds.absolute_left + bounds.absolute_right,
|
296
|
+
:height => BOX_MARGIN*2 + RHYTHM*2) do
|
297
|
+
|
298
|
+
fill_color LIGHT_GRAY
|
299
|
+
fill_rectangle([bounds.left, bounds.top],
|
300
|
+
bounds.right,
|
301
|
+
bounds.top - bounds.bottom)
|
302
|
+
fill_color BLACK
|
303
|
+
|
304
|
+
indent(BOX_MARGIN + INNER_MARGIN, &block)
|
305
|
+
end
|
306
|
+
|
307
|
+
stroke_color GRAY
|
308
|
+
stroke_horizontal_line(-BOX_MARGIN, bounds.width + BOX_MARGIN, :at => cursor)
|
309
|
+
stroke_color BLACK
|
310
|
+
|
311
|
+
move_down(RHYTHM*3)
|
312
|
+
end
|
313
|
+
|
314
|
+
# Renders a Bounding Box for the inner margin
|
315
|
+
#
|
316
|
+
def inner_box(&block)
|
317
|
+
bounding_box([INNER_MARGIN, cursor],
|
318
|
+
:width => bounds.width - INNER_MARGIN*2,
|
319
|
+
&block)
|
320
|
+
end
|
321
|
+
|
322
|
+
# Renders a Bounding Box with some background color and the formatted text
|
323
|
+
# inside it
|
324
|
+
#
|
325
|
+
def colored_box(box_text, options={})
|
326
|
+
options = { :fill_color => DARK_GRAY,
|
327
|
+
:stroke_color => nil,
|
328
|
+
:text_color => LIGHT_GRAY,
|
329
|
+
:leading => LEADING
|
330
|
+
}.merge(options)
|
331
|
+
|
332
|
+
register_fonts
|
333
|
+
text_options = { :leading => options[:leading],
|
334
|
+
:fallback_fonts => ["DejaVu", "Kai"]
|
335
|
+
}
|
336
|
+
|
337
|
+
box_height = height_of_formatted(box_text, text_options)
|
338
|
+
|
339
|
+
bounding_box([INNER_MARGIN + RHYTHM, cursor],
|
340
|
+
:width => bounds.width - (INNER_MARGIN+RHYTHM)*2) do
|
341
|
+
|
342
|
+
fill_color options[:fill_color]
|
343
|
+
stroke_color options[:stroke_color] || options[:fill_color]
|
344
|
+
fill_and_stroke_rounded_rectangle(
|
345
|
+
[bounds.left - RHYTHM, cursor],
|
346
|
+
bounds.left + bounds.right + RHYTHM*2,
|
347
|
+
box_height + RHYTHM*2,
|
348
|
+
5
|
349
|
+
)
|
350
|
+
fill_color BLACK
|
351
|
+
stroke_color BLACK
|
352
|
+
|
353
|
+
pad(RHYTHM) do
|
354
|
+
formatted_text(box_text, text_options)
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
move_down(RHYTHM*2)
|
359
|
+
end
|
360
|
+
|
361
|
+
# Draws X and Y axis rulers beginning at the margin box origin. Used on
|
362
|
+
# examples.
|
363
|
+
#
|
364
|
+
def stroke_axis(options={})
|
365
|
+
super({:height => (cursor - 20).to_i}.merge(options))
|
366
|
+
end
|
367
|
+
|
368
|
+
# Reset some of the Prawn settings including graphics and text to their
|
369
|
+
# defaults.
|
370
|
+
#
|
371
|
+
# Used after rendering examples so that each new example starts with a clean
|
372
|
+
# slate.
|
373
|
+
#
|
374
|
+
def reset_settings
|
375
|
+
|
376
|
+
# Text settings
|
377
|
+
font("Helvetica", :size => 12)
|
378
|
+
default_leading 0
|
379
|
+
self.text_direction = :ltr
|
380
|
+
|
381
|
+
# Graphics settings
|
382
|
+
self.line_width = 1
|
383
|
+
self.cap_style = :butt
|
384
|
+
self.join_style = :miter
|
385
|
+
undash
|
386
|
+
fill_color BLACK
|
387
|
+
stroke_color BLACK
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Prawn
|
4
|
+
module ManualBuilder
|
5
|
+
# The Prawn::ManualBuilder ExampleFile class is a utility class to ease
|
6
|
+
# the manipulation and extraction of source code and comments from the
|
7
|
+
# actual example files
|
8
|
+
#
|
9
|
+
class ExampleFile
|
10
|
+
attr_reader :package, :filename
|
11
|
+
|
12
|
+
# Stores the file data, filename and parent, which will be either an
|
13
|
+
# ExampleSection or an ExamplePackage.
|
14
|
+
#
|
15
|
+
# Available boolean options are:
|
16
|
+
#
|
17
|
+
# <tt>:eval_source</tt>:: Evals the example source code (default: true)
|
18
|
+
# <tt>:full_source</tt>:: Extract the full source code when true. Extract
|
19
|
+
# only the code between the generate block when false (default: false)
|
20
|
+
#
|
21
|
+
def initialize(parent, filename, options={})
|
22
|
+
@parent = parent.is_a?(String) ? ExamplePackage.new(parent) : parent
|
23
|
+
|
24
|
+
@filename = filename
|
25
|
+
@data = read_file(@parent.folder_name, filename)
|
26
|
+
|
27
|
+
@options = {:eval_source => true, :full_source => false}.merge(options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return the example source code excluding the initial comments and
|
31
|
+
# require calls
|
32
|
+
#
|
33
|
+
def full_source
|
34
|
+
@data.gsub(/# encoding.*?\n.*require.*?\n\n/m, "\n").strip
|
35
|
+
end
|
36
|
+
|
37
|
+
# Return the example source contained inside the first generate block or
|
38
|
+
# the full source if no generate block is found
|
39
|
+
#
|
40
|
+
def generate_block_source
|
41
|
+
block = @data.slice(/\w+\.generate.*? do\n(.*)end/m, 1)
|
42
|
+
|
43
|
+
return full_source unless block
|
44
|
+
|
45
|
+
block.gsub(/^( ){2}/, "")
|
46
|
+
end
|
47
|
+
|
48
|
+
# Return either the full_source or the generate_block_source according
|
49
|
+
# to the options
|
50
|
+
#
|
51
|
+
def source
|
52
|
+
@options[:full_source] ? full_source : generate_block_source
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return true if the example source should be evaluated inline within
|
56
|
+
# the manual according to the options
|
57
|
+
#
|
58
|
+
def eval?
|
59
|
+
@options[:eval_source]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Retrieve the comments between the encoding declaration and the require
|
63
|
+
# call for example_helper.rb
|
64
|
+
#
|
65
|
+
# Then removes the '#' signs, reflows the line breaks and return the result
|
66
|
+
#
|
67
|
+
def introduction_text
|
68
|
+
intro = @data.lines.grep(/^#/).join
|
69
|
+
intro.gsub!(/\n# (?=\S)/m, ' ')
|
70
|
+
intro.gsub!(/^#/, '')
|
71
|
+
intro.gsub!("\n", "\n\n")
|
72
|
+
intro.rstrip!
|
73
|
+
intro
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns a human friendly version of the example file name
|
77
|
+
#
|
78
|
+
def name
|
79
|
+
@name ||= @filename[/(.*)\.rb/, 1].gsub("_", " ").capitalize
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns this example's parent original folder name
|
83
|
+
#
|
84
|
+
def parent_folder_name
|
85
|
+
@parent.folder_name
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns the human friendly version of this example parent name
|
89
|
+
#
|
90
|
+
def parent_name
|
91
|
+
@parent.name
|
92
|
+
end
|
93
|
+
|
94
|
+
# Renders this example to a pdf
|
95
|
+
#
|
96
|
+
def render(pdf)
|
97
|
+
pdf.render_example(self)
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# Read the data from a file in a given package
|
103
|
+
#
|
104
|
+
def read_file(folder_name, filename)
|
105
|
+
data = File.read(File.expand_path(File.join(
|
106
|
+
Prawn::ManualBuilder.manual_dir, folder_name, filename)))
|
107
|
+
|
108
|
+
data.encode(::Encoding::UTF_8)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Prawn
|
4
|
+
module ManualBuilder
|
5
|
+
# The Prawn::ManualBuilder::ExamplePackage class is a utility class to
|
6
|
+
# handle the packaging of individual examples within a hierarchy
|
7
|
+
# when building the manual.
|
8
|
+
class ExamplePackage
|
9
|
+
attr_reader :intro_block, :folder_name
|
10
|
+
|
11
|
+
def initialize(folder_name)
|
12
|
+
@folder_name = folder_name
|
13
|
+
@hierarchy = []
|
14
|
+
end
|
15
|
+
|
16
|
+
# Stores a new ExampleSection in the hierarchy and yields it to a block
|
17
|
+
#
|
18
|
+
def section(name)
|
19
|
+
s = ExampleSection.new(self, name)
|
20
|
+
yield s
|
21
|
+
@hierarchy << s
|
22
|
+
end
|
23
|
+
|
24
|
+
# Stores a new ExampleFile in the hierarchy
|
25
|
+
#
|
26
|
+
def example(filename, options={})
|
27
|
+
@hierarchy << ExampleFile.new(self, "#{filename}.rb", options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Stores a block with code to be evaluated when rendering the package cover
|
31
|
+
#
|
32
|
+
def intro(&block)
|
33
|
+
@intro_block = block
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns a human friendly version of the package name
|
37
|
+
#
|
38
|
+
def name
|
39
|
+
@name ||= @folder_name.gsub("_", " ").capitalize
|
40
|
+
end
|
41
|
+
|
42
|
+
# Renders a cover page for the package to a pdf and iterates the examples
|
43
|
+
# hierarchy delegating the examples and sections to be rendered as well
|
44
|
+
#
|
45
|
+
def render(pdf)
|
46
|
+
pdf.render_package_cover(self)
|
47
|
+
|
48
|
+
@hierarchy.each do |node|
|
49
|
+
node.render(pdf)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Prawn
|
4
|
+
module ManualBuilder
|
5
|
+
# The Prawn::ManualBuilder::ExampleSection class is a utility class to
|
6
|
+
# handle sections of related examples within an ExamplePackage
|
7
|
+
#
|
8
|
+
class ExampleSection
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
def initialize(package, name)
|
12
|
+
@package = package
|
13
|
+
@name = name
|
14
|
+
@examples = []
|
15
|
+
end
|
16
|
+
|
17
|
+
# Stores a new ExampleFile in the examples list
|
18
|
+
#
|
19
|
+
def example(filename, options={})
|
20
|
+
@examples << ExampleFile.new(self, "#{filename}.rb", options)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns this example's package original folder name
|
24
|
+
#
|
25
|
+
def folder_name
|
26
|
+
@package.folder_name
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the human friendly version of this section's package name
|
30
|
+
#
|
31
|
+
def package_name
|
32
|
+
@package.name
|
33
|
+
end
|
34
|
+
|
35
|
+
# Renders the section to a pdf and iterates the examples list delegating the
|
36
|
+
# examples to be rendered as well
|
37
|
+
#
|
38
|
+
def render(pdf)
|
39
|
+
pdf.render_section(self)
|
40
|
+
|
41
|
+
@examples.each do |example|
|
42
|
+
example.render(pdf)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "coderay"
|
4
|
+
|
5
|
+
module Prawn
|
6
|
+
module ManualBuilder
|
7
|
+
# Registers a to_prawn method on CodeRay. It returns an array of hashes to be
|
8
|
+
# used with formatted_text.
|
9
|
+
#
|
10
|
+
# Usage:
|
11
|
+
#
|
12
|
+
# CodeRay.scan(string, :ruby).to_prawn
|
13
|
+
#
|
14
|
+
class PrawnEncoder < CodeRay::Encoders::Encoder
|
15
|
+
register_for :to_prawn
|
16
|
+
|
17
|
+
COLORS = { :default => "FFFFFF",
|
18
|
+
|
19
|
+
:comment => "AEAEAE",
|
20
|
+
:constant => "88A5D2",
|
21
|
+
:instance_variable => "E8ED97",
|
22
|
+
:integer => "C8FF0E",
|
23
|
+
:float => "C8FF0E",
|
24
|
+
:inline_delimiter => "EF804F", # #{} within a string
|
25
|
+
:keyword => "FEE100",
|
26
|
+
|
27
|
+
# BUG: There appear to be some problem with this token. Method
|
28
|
+
# definitions are considered as ident tokens
|
29
|
+
#
|
30
|
+
:method => "FF5C00",
|
31
|
+
:string => "56D65E",
|
32
|
+
:symbol => "C8FF0E"
|
33
|
+
}
|
34
|
+
|
35
|
+
def setup(options)
|
36
|
+
super
|
37
|
+
@out = []
|
38
|
+
@open = []
|
39
|
+
end
|
40
|
+
|
41
|
+
def text_token(text, kind)
|
42
|
+
color = COLORS[kind] || COLORS[@open.last] || COLORS[:default]
|
43
|
+
|
44
|
+
@out << {:text => text, :color => color}
|
45
|
+
end
|
46
|
+
|
47
|
+
def begin_group(kind)
|
48
|
+
@open << kind
|
49
|
+
end
|
50
|
+
|
51
|
+
def end_group(kind)
|
52
|
+
@open.pop
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prawn-manual_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Felipe Doria
|
8
|
+
- Gregory Brown
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: coderay
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 1.0.7
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 1.0.7
|
28
|
+
description: A tool for writing manuals for Prawn and Prawn accessories
|
29
|
+
email:
|
30
|
+
- felipe.doria@gmail.com
|
31
|
+
- gregory.t.brown@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- README.md
|
37
|
+
- data/fonts/DejaVuSans.ttf
|
38
|
+
- data/fonts/Dustismo_Roman.ttf
|
39
|
+
- data/fonts/gkai00mp.ttf
|
40
|
+
- lib/prawn/manual_builder.rb
|
41
|
+
- lib/prawn/manual_builder/example.rb
|
42
|
+
- lib/prawn/manual_builder/example_file.rb
|
43
|
+
- lib/prawn/manual_builder/example_package.rb
|
44
|
+
- lib/prawn/manual_builder/example_section.rb
|
45
|
+
- lib/prawn/manual_builder/syntax_highlight.rb
|
46
|
+
homepage:
|
47
|
+
licenses: []
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.9.3
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.2.2
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: A tool for writing manuals for Prawn and Prawn accessories
|
69
|
+
test_files: []
|
70
|
+
has_rdoc:
|