Skip to content

Commit e7f93e2

Browse files
committed
Complete coffee -> JS conversion of docs
1 parent 6f0d5ff commit e7f93e2

File tree

9 files changed

+245
-248
lines changed

9 files changed

+245
-248
lines changed

docs/annotations.coffee.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ covered by another annotation and the user won't be able to click it.
3737

3838
Here is an example that uses a few of the annotation types.
3939

40-
# Add the link text
40+
// Add the link text
4141
doc.fontSize(25)
4242
.fillColor('blue')
43-
.text('This is a link!', 20, 0)
43+
.text('This is a link!', 20, 0);
4444
45-
# Measure the text
46-
width = doc.widthOfString('This is a link!')
47-
height = doc.currentLineHeight()
45+
// Measure the text
46+
const width = doc.widthOfString('This is a link!');
47+
const height = doc.currentLineHeight();
4848
49-
# Add the underline and link annotations
50-
doc.underline(20, 0, width, height, color: 'blue')
51-
.link(20, 0, width, height, 'http://google.com/')
49+
// Add the underline and link annotations
50+
doc.underline(20, 0, width, height, {color: 'blue'})
51+
.link(20, 0, width, height, 'http://google.com/');
5252
53-
# Create the highlighted text
53+
// Create the highlighted text
5454
doc.moveDown()
5555
.fillColor('black')
5656
.highlight(20, doc.y, doc.widthOfString('This text is highlighted!'), height)
57-
.text('This text is highlighted!')
57+
.text('This text is highlighted!');
5858
59-
# Create the crossed out text
59+
// Create the crossed out text
6060
doc.moveDown()
6161
.strike(20, doc.y, doc.widthOfString('STRIKE!'), height)
62-
.text('STRIKE!')
62+
.text('STRIKE!');
6363
6464
The output of this example looks like this.
6565

@@ -70,11 +70,13 @@ that is the fault of the PDF spec itself. Calculating a rectangle manually isn't
7070
fun, but PDFKit makes it easier for a few common annotations applied to text, including
7171
links, underlines, and strikes. Here's an example showing two of them:
7272

73-
doc.fontSize 20
74-
.fillColor 'red'
75-
.text 'Another link!', 20, 0,
73+
doc.fontSize(20)
74+
.fillColor('red')
75+
.text('Another link!', 20, 0, {
7676
link: 'http://apple.com/',
7777
underline: true
78+
}
79+
);
7880
7981
The output is as you'd expect:
8082

docs/generate.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
const fs = require('fs');
1010
const vm = require('vm');
1111
const md = require('markdown').markdown;
12-
const coffee = require('coffee-script');
1312
const CodeMirror = require('codemirror/addon/runmode/runmode.node');
1413
const PDFDocument = require('../');
1514

1615
process.chdir(__dirname);
1716

18-
// setup code mirror coffeescript mode
19-
const filename = require.resolve('codemirror/mode/coffeescript/coffeescript');
20-
const coffeeMode = fs.readFileSync(filename, 'utf8');
21-
vm.runInNewContext(coffeeMode, {CodeMirror});
17+
// setup code mirror javascript mode
18+
const filename = require.resolve('codemirror/mode/javascript/javascript');
19+
const jsMode = fs.readFileSync(filename, 'utf8');
20+
vm.runInNewContext(jsMode, {CodeMirror});
2221

2322
// style definitions for markdown
2423
const styles = {
@@ -142,7 +141,7 @@ class Node {
142141
// use code mirror to syntax highlight the code block
143142
var code = this.content[0].text;
144143
this.content = [];
145-
CodeMirror.runMode(code, 'coffeescript', (text, style) => {
144+
CodeMirror.runMode(code, 'javascript', (text, style) => {
146145
const color = colors[style] || colors.default;
147146
const opts = {
148147
color,
@@ -158,11 +157,11 @@ class Node {
158157

159158
case 'img':
160159
// images are used to generate inline example output
161-
// compiles the coffeescript to JS so it can be run
160+
// stores the JS so it can be run
162161
// in the render method
163162
this.type = 'example';
164163
code = codeBlocks[this.attrs.alt];
165-
if (code) { this.code = coffee.compile(code); }
164+
if (code) { this.code = code; }
166165
this.height = +this.attrs.title || 0;
167166
break;
168167
}
@@ -266,7 +265,7 @@ class Node {
266265
}
267266
}
268267

269-
// reads and renders a markdown/literate coffeescript file to the document
268+
// reads and renders a markdown/literate javascript file to the document
270269
const render = function(doc, filename) {
271270
codeBlocks = [];
272271
const tree = md.parse(fs.readFileSync(filename, 'utf8'));

docs/generate_website.js

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
/*
2-
* decaffeinate suggestions:
3-
* DS101: Remove unnecessary use of Array.from
4-
* DS102: Remove unnecessary code created because of implicit returns
5-
* DS205: Consider reworking code to avoid use of IIFEs
6-
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
7-
*/
81
const jade = require('jade');
92
const { markdown } = require('markdown');
103
const fs = require('fs');
114
const vm = require('vm');
12-
const coffee = require('coffee-script');
135
const {exec} = require('child_process');
146
const PDFDocument = require('../');
157

@@ -53,68 +45,58 @@ let imageIndex = 0;
5345
const generateImages = function(tree) {
5446
// find code blocks
5547
const codeBlocks = [];
56-
for (var node of Array.from(tree)) {
48+
for (var node of tree) {
5749
if (node[0] === 'code_block') {
5850
codeBlocks.push(node[1]);
5951
}
6052
}
6153

62-
return (() => {
63-
const result = [];
64-
for (node of Array.from(tree)) {
65-
if ((node[0] === 'para') && Array.isArray(node[1]) && (node[1][0] === 'img')) {
66-
// compile the code
67-
const attrs = node[1][1];
68-
let code = codeBlocks[attrs.alt];
69-
if (code) { code = coffee.compile(code); }
70-
delete attrs.height; // used for pdf generation
71-
72-
// create a PDF and run the example
73-
const doc = new PDFDocument;
74-
const f = `img/${imageIndex++}`;
75-
var file = fs.createWriteStream(`${f}.pdf`);
76-
doc.pipe(file);
77-
78-
doc.translate(doc.x, doc.y);
79-
doc.scale(0.8);
80-
doc.x = (doc.y = 0);
81-
82-
vm.runInNewContext(code, {
83-
doc,
84-
lorem
85-
}
86-
);
87-
88-
delete attrs.title;
89-
delete attrs.alt;
90-
attrs.href = `${f}.png`;
54+
for (node of tree) {
55+
if ((node[0] === 'para') && Array.isArray(node[1]) && (node[1][0] === 'img')) {
56+
// compile the code
57+
const attrs = node[1][1];
58+
let code = codeBlocks[attrs.alt];
59+
delete attrs.height; // used for pdf generation
60+
61+
// create a PDF and run the example
62+
const doc = new PDFDocument;
63+
const f = `img/${imageIndex++}`;
64+
var file = fs.createWriteStream(`${f}.pdf`);
65+
doc.pipe(file);
66+
67+
doc.translate(doc.x, doc.y);
68+
doc.scale(0.8);
69+
doc.x = (doc.y = 0);
9170

92-
// write the PDF, convert to PNG using the mac `sips`
93-
// command line tool, and trim with graphicsmagick
94-
(f =>
95-
file.on('finish', () =>
96-
exec(`sips -s format png ${f}.pdf --out ${f}.png`, function() {
97-
fs.unlink(`${f}.pdf`);
98-
return exec(`gm convert ${f}.png -trim ${f}.png`);
99-
})
100-
)
101-
)(f);
102-
103-
result.push(doc.end());
104-
} else {
105-
result.push(undefined);
71+
vm.runInNewContext(code, {
72+
doc,
73+
lorem
10674
}
75+
);
76+
77+
delete attrs.title;
78+
delete attrs.alt;
79+
attrs.href = `${f}.png`;
80+
81+
// write the PDF, convert to PNG using the mac `sips`
82+
// command line tool, and trim with graphicsmagick
83+
84+
file.on('finish', () =>
85+
exec(`sips -s format png ${f}.pdf --out ${f}.png`, function() {
86+
fs.unlink(`${f}.pdf`);
87+
exec(`gm convert ${f}.png -trim ${f}.png`);
88+
})
89+
)
10790
}
108-
return result;
109-
})();
91+
}
11092
};
11193

11294
const pages = [];
11395
for (let file of Array.from(files)) {
11496
let content = fs.readFileSync(file, 'utf8');
11597

11698
// turn github highlighted code blocks into normal markdown code blocks
117-
content = content.replace(/^```coffeescript\n((:?.|\n)*?)\n```/mg, (m, $1) => ` ${$1.split('\n').join('\n ')}`);
99+
content = content.replace(/^```javascript\n((:?.|\n)*?)\n```/mg, (m, $1) => ` ${$1.split('\n').join('\n ')}`);
118100

119101
const tree = markdown.parse(content);
120102
const headers = extractHeaders(tree);

docs/getting_started.coffee.md

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ Creating a PDFKit document is quite simple. Just require the `pdfkit` module
1313
in your CoffeeScript or JavaScript source file and create an instance of the
1414
`PDFDocument` class.
1515

16-
PDFDocument = require 'pdfkit'
17-
doc = new PDFDocument
16+
const PDFDocument = require('pdfkit');
17+
const doc = new PDFDocument;
1818
1919
`PDFDocument` instances are readable Node streams. They don't get saved anywhere automatically,
2020
but you can call the `pipe` method to send the output of the PDF document to another
2121
writable Node stream as it is being written. When you're done with your document, call
2222
the `end` method to finalize it. Here is an example showing how to pipe to a file or an HTTP response.
2323

24-
doc.pipe fs.createWriteStream('/path/to/file.pdf') # write to PDF
25-
doc.pipe res # HTTP response
24+
doc.pipe(fs.createWriteStream('/path/to/file.pdf')); // write to PDF
25+
doc.pipe(res); // HTTP response
2626
27-
# add stuff to PDF here using methods described below...
27+
// add stuff to PDF here using methods described below...
2828
29-
# finalize the PDF and end the stream
30-
doc.end()
29+
// finalize the PDF and end the stream
30+
doc.end();
3131
3232
The `write` and `output` methods found in PDFKit before version 0.5 are now deprecated.
3333

@@ -48,27 +48,28 @@ To get a Blob from a `PDFDocument`, you should pipe it to a [blob-stream](https:
4848
which is a module that generates a Blob from any Node-style stream. The following example uses Browserify to load
4949
`PDFKit` and `blob-stream`, but if you're not using Browserify, you can load them in whatever way you'd like (e.g. script tags).
5050

51-
# require dependencies
52-
PDFDocument = require 'pdfkit'
53-
blobStream = require 'blob-stream'
51+
// require dependencies
52+
const PDFDocument = require('pdfkit');
53+
const blobStream = require('blob-stream');
5454
55-
# create a document the same way as above
56-
doc = new PDFDocument
55+
// create a document the same way as above
56+
const doc = new PDFDocument;
5757
58-
# pipe the document to a blob
59-
stream = doc.pipe(blobStream())
58+
// pipe the document to a blob
59+
const stream = doc.pipe(blobStream());
6060
61-
# add your content to the document here, as usual
61+
// add your content to the document here, as usual
6262
63-
# get a blob when you're done
64-
doc.end()
65-
stream.on 'finish', ->
66-
# get a blob you can do whatever you like with
67-
blob = stream.toBlob('application/pdf')
63+
// get a blob when you're done
64+
doc.end();
65+
stream.on('finish', function() {
66+
// get a blob you can do whatever you like with
67+
const blob = stream.toBlob('application/pdf');
6868
69-
# or get a blob URL for display in the browser
70-
url = stream.toBlobURL('application/pdf')
71-
iframe.src = url
69+
// or get a blob URL for display in the browser
70+
const url = stream.toBlobURL('application/pdf');
71+
iframe.src = url;
72+
});
7273
7374
You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
7475
@@ -87,8 +88,7 @@ quite simple!
8788
8889
To add some content every time a page is created, either by calling `addPage()` or automatically, you can use the `pageAdded` event.
8990
90-
doc.on 'pageAdded', ->
91-
doc.text "Page Title"
91+
doc.on('pageAdded', () => doc.text("Page Title"));
9292
9393
You can also set some options for the page, such as its size and orientation.
9494
@@ -110,18 +110,20 @@ on all sides.
110110
111111
For example:
112112
113-
# Add a 50 point margin on all sides
114-
doc.addPage
115-
margin: 50
113+
// Add a 50 point margin on all sides
114+
doc.addPage({
115+
margin: 50});
116116
117117
118-
# Add different margins on each side
119-
doc.addPage
120-
margins:
121-
top: 50
122-
bottom: 50
123-
left: 72
118+
// Add different margins on each side
119+
doc.addPage({
120+
margins: {
121+
top: 50,
122+
bottom: 50,
123+
left: 72,
124124
right: 72
125+
}
126+
});
125127
126128
## Switching to previous pages
127129
@@ -142,28 +144,31 @@ never need to call it. Finally, there is a `bufferedPageRange` method, which re
142144
of pages that are currently buffered. Here is a small example that shows how you might add page
143145
numbers to a document.
144146

145-
# create a document, and enable bufferPages mode
146-
doc = new PDFDocument
147-
bufferPages: true
147+
// create a document, and enable bufferPages mode
148+
let i;
149+
let end;
150+
const doc = new PDFDocument({
151+
bufferPages: true});
148152
149-
# add some content...
150-
doc.addPage()
151-
# ...
152-
doc.addPage()
153+
// add some content...
154+
doc.addPage();
155+
// ...
156+
doc.addPage();
153157
154-
# see the range of buffered pages
155-
range = doc.bufferedPageRange() # => { start: 0, count: 2 }
158+
// see the range of buffered pages
159+
const range = doc.bufferedPageRange(); // => { start: 0, count: 2 }
156160
157-
for i in [range.start...range.start + range.count]
158-
doc.switchToPage(i)
159-
doc.text "Page #{i + 1} of #{range.count}"
161+
for (i = range.start, end = range.start + range.count, range.start <= end; i < end; i++;) {
162+
doc.switchToPage(i);
163+
doc.text(`Page ${i + 1} of ${range.count}`);
164+
}
160165
161-
# manually flush pages that have been buffered
162-
doc.flushPages()
166+
// manually flush pages that have been buffered
167+
doc.flushPages();
163168
164-
# or, if you are at the end of the document anyway,
165-
# doc.end() will call it for you automatically.
166-
doc.end()
169+
// or, if you are at the end of the document anyway,
170+
// doc.end() will call it for you automatically.
171+
doc.end();
167172
168173
## Setting document metadata
169174

0 commit comments

Comments
 (0)