Print a GitHub repository's code as .docx or .pdf files.
When diving into a codebase, I like to have physical copies of the source code. It's easier on my eyes - especially after a long day of programming screen time.
Before, I'd manually copy and paste the content of a code file into MS Word, attach page numbers and a title (filename), and then convert it into pdf (using Word or online tools, etc.). Not much of an issue at all. Until the file count grew. Even repeating those steps for more than a few files felt ... hard-coded.
This is my attempt to automate the menial process. Of course, screen time is inevitable to a programmer (and non-coders alike). So, this solution may seem like a drop in the ocean, but hopefully more folks will come to appreciate physical source code study.
$ npm install print-lib
$ pip install -r node_modules/print-lib/requirements.txt
python >= 3.8
Nodejs >= 14
- MS Word installed on local machine (needed for
.pdf
conversion)
const { printLib } = require("print-lib");
(async () => {
const url = "https://github.com/Zen-cronic/scope-logger/tree/main/src";
await printLib({
link: url,
linkType: "recursive",
auth: process.env.API_ACCESS_TOKEN,
userAgent: "Zen-cronic",
convertTo: "pdf",
});
})();
Output:
Prints 3 directories in the root:
-
The code files in
code
-
The .docx files in
word
-
The .pdf files in
pdf
(This is optional; see theConfiguring Options
section )
All are required.
-
link <string>
: A valid github repository url -
linkType <"file"|"dir"|"recursive">
: The type of the content represented by thelink
.-
A.
file
: A single source code file. For example, the link representsindex.js
only. -
B.
dir
: All the files in the given directory. Ignores files in sub-directories. -
C.
recursive
: Recursively traverse all files in a given directory, including sub-directories.
-
-
auth
: The GitHub API token needed to make the requests to fetch repository content. Without this, the rate limit is very low. Fine-grain access tokens are recommended. Check theResources
section for more information. -
userAgent
: Your GitHub username or the name of your GitHub App. -
convertTo <"word"|"pdf">
: The stage of the file conversions.-
A.
word
: Generate.docx
files from the files incode
directory. Works cross-platform. -
B.
pdf
: Generate.pdf
files from the files inword
directory. Only available on machines with MS Word installed. Therefore, it cannot be used on linux. If your local machine is not running on Windows or MacOS and this option is set, it fallbacks toword
.
-
- 0 javascript/nodejs dependency!
python-docx
for.docx
conversiondocx2pdf
for.pdf
conversion
Uses dynamic imports in testing with jest. Therefore, the --experimental-vm-modules
flag must be set.
$ npm run test:dynamic