Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
364 lines (316 loc) · 7.94 KB
/
Copy pathexample_test.go
File metadata and controls
364 lines (316 loc) · 7.94 KB
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
84
85
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
247
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package picoloom_test
import (
"context"
"fmt"
"strings"
"sync"
"github.com/infinilabs/picoloom/v2"
)
// Example demonstrates basic markdown to HTML conversion.
// For PDF output, set HTMLOnly to false (requires Chrome).
func Example() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Hello World\n\nThis is a test.",
HTMLOnly: true, // Skip PDF generation for this example
})
if err != nil {
fmt.Println("error:", err)
return
}
// Check that HTML was generated
if strings.Contains(string(result.HTML), "<h1") {
fmt.Println("HTML generated successfully")
}
// Output: HTML generated successfully
}
// Example_withCover demonstrates adding a cover page.
func Example_withCover() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Introduction\n\nDocument content here.",
Cover: &picoloom.Cover{
Title: "Project Report",
Subtitle: "Q4 2025 Analysis",
Author: "John Doe",
Organization: "Acme Corp",
Date: "2025-12-15",
Version: "v1.0",
},
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if strings.Contains(string(result.HTML), "Project Report") {
fmt.Println("Cover page generated")
}
// Output: Cover page generated
}
// Example_withTOC demonstrates adding a table of contents.
func Example_withTOC() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
markdown := `# Document Title
## Chapter 1
Content for chapter 1.
## Chapter 2
Content for chapter 2.
### Section 2.1
Subsection content.
`
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: markdown,
TOC: &picoloom.TOC{
Title: "Contents",
MinDepth: 2, // Start at h2 (skip document title)
MaxDepth: 3, // Include up to h3
},
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if strings.Contains(string(result.HTML), "toc") {
fmt.Println("TOC generated")
}
// Output: TOC generated
}
// Example_withCustomCSS demonstrates injecting custom CSS.
func Example_withCustomCSS() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Styled Document\n\nCustom styling applied.",
CSS: `
body { font-family: Georgia, serif; }
h1 { color: #2c3e50; border-bottom: 2px solid #3498db; }
`,
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if strings.Contains(string(result.HTML), "Georgia") {
fmt.Println("Custom CSS injected")
}
// Output: Custom CSS injected
}
// Example_withSignature demonstrates adding a signature block.
func Example_withSignature() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Report\n\nDocument content.",
Signature: &picoloom.Signature{
Name: "Jane Smith",
Title: "Senior Engineer",
Email: "jane@example.com",
Organization: "Tech Corp",
},
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if strings.Contains(string(result.HTML), "Jane Smith") {
fmt.Println("Signature block added")
}
// Output: Signature block added
}
// Example_withWatermark demonstrates adding a watermark.
func Example_withWatermark() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Draft Document\n\nThis is a draft.",
Watermark: &picoloom.Watermark{
Text: "DRAFT",
Color: "#888888",
Opacity: 0.1,
Angle: -45,
},
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if strings.Contains(string(result.HTML), "DRAFT") {
fmt.Println("Watermark CSS generated")
}
// Output: Watermark CSS generated
}
// Example_withPageSettings demonstrates configuring page settings.
func Example_withPageSettings() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# A4 Document\n\nConfigured for A4 paper.",
Page: &picoloom.PageSettings{
Size: picoloom.PageSizeA4,
Orientation: picoloom.OrientationPortrait,
Margin: 1.0, // inches
},
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if len(result.HTML) > 0 {
fmt.Println("Page settings configured")
}
// Output: Page settings configured
}
// Example_withFooter demonstrates adding a page footer.
func Example_withFooter() {
conv, err := picoloom.NewConverter()
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Document with Footer\n\nContent here.",
Footer: &picoloom.Footer{
Position: "center",
ShowPageNumber: true,
Date: "2025-01-15",
Text: "Confidential",
},
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if len(result.HTML) > 0 {
fmt.Println("Footer configured")
}
// Output: Footer configured
}
// ExampleNewConverter_withStyle demonstrates using a built-in style.
func ExampleNewConverter_withStyle() {
conv, err := picoloom.NewConverter(picoloom.WithStyle("technical"))
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Technical Document\n\nUsing the technical style.",
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
// Technical style uses system-ui font
if strings.Contains(string(result.HTML), "system-ui") {
fmt.Println("Technical style applied")
}
// Output: Technical style applied
}
// ExampleConverterPool demonstrates parallel batch processing.
func ExampleConverterPool() {
pool := picoloom.NewConverterPool(2)
// Process two documents in parallel
docs := []string{
"# Document 1\n\nFirst document.",
"# Document 2\n\nSecond document.",
}
// Channel to collect results, WaitGroup to synchronize goroutines
results := make(chan bool, len(docs))
var wg sync.WaitGroup
for _, doc := range docs {
wg.Add(1)
go func(markdown string) {
defer wg.Done()
conv := pool.Acquire()
if conv == nil {
results <- false
return
}
defer pool.Release(conv)
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: markdown,
HTMLOnly: true,
})
results <- err == nil && strings.Contains(string(result.HTML), "Document")
}(doc)
}
// Wait for all goroutines to finish before closing pool
wg.Wait()
pool.Close()
// Collect results
success := 0
for range docs {
if <-results {
success++
}
}
fmt.Printf("Processed %d documents\n", success)
// Output: Processed 2 documents
}
// ExampleNewAssetLoader demonstrates loading custom assets.
func ExampleNewAssetLoader() {
// NewAssetLoader with empty path uses embedded assets only
loader, err := picoloom.NewAssetLoader("")
if err != nil {
fmt.Println("error:", err)
return
}
conv, err := picoloom.NewConverter(picoloom.WithAssetLoader(loader))
if err != nil {
fmt.Println("error:", err)
return
}
defer conv.Close()
result, err := conv.Convert(context.Background(), picoloom.Input{
Markdown: "# Custom Assets\n\nUsing asset loader.",
HTMLOnly: true,
})
if err != nil {
fmt.Println("error:", err)
return
}
if len(result.HTML) > 0 {
fmt.Println("Asset loader configured")
}
// Output: Asset loader configured
}