Skip to content

Commit

Permalink
scripts: build and deploy PDF pages for translations (#10846)
Browse files Browse the repository at this point in the history
* scrips: build and deploy PDF pages for all languages

* cleanup/render.py: reformat code

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* Apply suggestions from code review

Co-authored-by: Matthew Peveler <[email protected]>

* scrpts/pdf: update README, refactor code

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* test/ci: building PDF was wildcard

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* test/ci: building translations as wildcard 2

* test/ci: fix flag in PDF building

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* test/ci: update build pdf action

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* test/ci: extend PDF exclusion list

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* cleanup/ci: update PDF translation build

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* scripts/pdf: add website and repo link

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* test/ci: move PDF build to seperate script file

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* test/ci: minor fixes to build pdf script

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* cleanup/ci: update build PDF

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* scripts: update font family, minor fix

Signed-off-by: K.B.Dharun Krishna <[email protected]>

* fix/deploy: sha256sum command

Signed-off-by: K.B.Dharun Krishna <[email protected]>

---------

Signed-off-by: K.B.Dharun Krishna <[email protected]>
Co-authored-by: Matthew Peveler <[email protected]>
  • Loading branch information
kbdharun and MasterOdin authored Oct 13, 2023
1 parent 7856124 commit a2ab577
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: Build PDF
if: github.repository == 'tldr-pages/tldr' && github.ref == 'refs/heads/main'
working-directory: ./scripts/pdf
run: python render.py ../../pages -c solarized-light
run: bash build-pdf.sh

- name: Deploy
if: github.repository == 'tldr-pages/tldr' && github.ref == 'refs/heads/main'
Expand Down
5 changes: 3 additions & 2 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ function upload_assets {
mv -f "${TLDR_LANG_ARCHIVES_DIRECTORY}"/*.zip "$SITE_HOME/assets/"
rm -rf "$TLDR_LANG_ARCHIVES_DIRECTORY"
cp -f "$TLDRHOME/index.json" "$SITE_HOME/assets/"
cp -f "${TLDRHOME}/scripts/pdf/tldr-pages.pdf" "${SITE_HOME}/assets/tldr-book.pdf"
cp -f "${TLDRHOME}/scripts/pdf/tldr-book*.pdf" "${SITE_HOME}/assets/"


sha256sum \
"${SITE_HOME}/assets/index.json" \
"${SITE_HOME}/assets/"*.zip \
"${SITE_HOME}/assets/tldr-book.pdf" \
"${SITE_HOME}/assets/tldr-book*.pdf" \
> "${SITE_HOME}/assets/tldr.sha256sums"

cd "$SITE_HOME"
Expand Down
Binary file added scripts/pdf/NotoSans-Regular.ttf
Binary file not shown.
22 changes: 11 additions & 11 deletions scripts/pdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

This directory contains the script and related resources to generate a PDF document with all the `tldr` pages.

## Preview

![cryptsetup in the Basic color-scheme.](https://user-images.githubusercontent.com/29029116/35637791-4e42af80-06db-11e8-8b8e-42ce6c905ff4.jpg)
![cryptsetup in the Solarized Light color-scheme.](https://user-images.githubusercontent.com/29029116/35637798-51e3784a-06db-11e8-9576-6e57ef5c5c20.jpg)
![cryptsetup in the Solarized Dark color-scheme.](https://user-images.githubusercontent.com/29029116/35637801-54449fce-06db-11e8-93f7-d90cdc34044b.jpg)

## Highlights

- No LaTeX dependencies for generating the PDF.
Expand All @@ -25,14 +19,20 @@ Make sure OS specific dependencies for WeasyPrint are installed by following the

Generating the PDF is as simple as running:

python3 render.py <path-to-pages-directory> --color <color-scheme>
python3 render.py <path-to-pages-directory> [--color <color-scheme>] [--output <filename>]

Complete information about the arguments can be viewed by running:

python3 render.py --help

The color-schemes that can be specified are:
Available color schemes:

* `basic`
* `solarized-light`
* `solarized-dark`
- `basic`
- `solarized-light`
- `solarized-dark`

## Preview

![cryptsetup in the Basic color-scheme.](https://user-images.githubusercontent.com/29029116/35637791-4e42af80-06db-11e8-8b8e-42ce6c905ff4.jpg)
![cryptsetup in the Solarized Light color-scheme.](https://user-images.githubusercontent.com/29029116/35637798-51e3784a-06db-11e8-9576-6e57ef5c5c20.jpg)
![cryptsetup in the Solarized Dark color-scheme.](https://user-images.githubusercontent.com/29029116/35637801-54449fce-06db-11e8-93f7-d90cdc34044b.jpg)
6 changes: 3 additions & 3 deletions scripts/pdf/basic.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* SPDX-License-Identifier: MIT */

@font-face {
font-family: "PT Serif";
src: url("pt-serif-web-regular.ttf") format("truetype");
font-family: "sans-serif";
src: url("NotoSans-Regular.ttf") format("truetype");
}

p {
Expand All @@ -14,7 +14,7 @@ code {
}

h1, h2, h4, ul {
font-family: "PT Serif";
font-family: "sans-serif";
}

.title-main {
Expand Down
33 changes: 33 additions & 0 deletions scripts/pdf/build-pdf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT

# This script is executed by GitHub Actions when a PR is merged (i.e. in the `Build PDF` step).
set -ex

function process_page {
pageDir="$1"
folder=$(basename "${pageDir}")
language="${folder##*.}"
case $folder in
pages.bn | pages.ja | pages.ko | pages.ml | pages.ta | pages.th | pages.zh | pages.zh_TW)
;;
pages)
python3 render.py "${pageDir}" -c solarized-light
;;
*)
python3 render.py "${pageDir}" -c basic -o "tldr-book-${language}.pdf"
;;
esac
}

function main {
for pageDir in ../../pages*; do
process_page "${pageDir}"
done
}

###################################
# MAIN
###################################

main
Binary file removed scripts/pdf/pt-serif-web-regular.ttf
Binary file not shown.
22 changes: 15 additions & 7 deletions scripts/pdf/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from weasyprint import HTML


def main(loc, colorscheme):
def main(loc, colorscheme, output_filename):
# Checking correctness of path
if not os.path.isdir(loc):
print("Invalid directory. Please try again!", file=sys.stderr)
Expand All @@ -31,11 +31,13 @@ def main(loc, colorscheme):
# A string that stores all pages in HTML format
html = (
'<!doctype html><html><head><meta charset="utf-8"></head>'
+ "<body><h1 class=title-main>tldr pages</h1>"
+ "<body><h1 class=title-main>tldr pages book</h1>"
+ "<div class=title-sub>Simplified and community-driven man pages</div>"
+ "<div class=title-sub><em><small>Generated on "
+ datetime.now().strftime("%c")
+ "</small></em></div>"
+ "</small></em></div><br><br>"
+ "<div class=title-sub>Website: <a href=https://tldr.sh>https://tldr.sh</a></div><br>"
+ "<div class=title-sub>GitHub: <a href=https://github.com/tldr-pages/tldr>https://github.com/tldr-pages/tldr</a></div><br>"
+ '<p style="page-break-before: always" ></p>'
)

Expand Down Expand Up @@ -69,10 +71,10 @@ def main(loc, colorscheme):

# Writing the PDF to disk
print("\nConverting all pages to PDF...")
HTML(string=html).write_pdf("tldr-pages.pdf", stylesheets=csslist)
HTML(string=html).write_pdf(output_filename, stylesheets=csslist)

if os.path.exists("tldr-pages.pdf"):
print("\nCreated tldr-pages.pdf in the current directory!\n")
if os.path.exists(output_filename):
print(f"\nCreated {output_filename} in the current directory!\n")


if __name__ == "__main__":
Expand All @@ -89,6 +91,12 @@ def main(loc, colorscheme):
default="basic",
help="Color scheme of the PDF",
)
parser.add_argument(
"-o",
"--output",
default="tldr-book.pdf",
help="Custom filename for the output PDF (default is 'tldr-pages.pdf')",
)
args = parser.parse_args()

main(args.dir_path, args.color)
main(args.dir_path, args.color, args.output)

0 comments on commit a2ab577

Please sign in to comment.