Skip to content

Commit

Permalink
feat: adapt section indent and babel packages for languages
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Jun 28, 2024
1 parent 72d80c3 commit 2247a5b
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
java-version: '11'
- run: |
PANDOC_URL=$(curl https://api.github.com/repos/jgm/pandoc/releases/latest | jq -r ".assets[] | select(.name | test(\"amd64.deb$\")) | .browser_download_url")
curl --silent --show-error --location --fail --retry 4 --retry-delay 5 --output pandoc.deb $PANDOC_URL
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pdf:
rights: 'ISBN: 123-4-5678901-6-7'
# latex packages option is only used by pdf format
latexPackages:
- '[frenchb]{babel}'
- '[french]{babel}'
```
### Commands
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"lint": "eslint . --cache",
"lint:fix": "eslint . --fix",
"pretest": "node scripts/github-releases.js",
"test": "npm-run-all pretest test:unit test:acceptance",
"pretest:acceptance": "find ./test -type f \\( -iname '*.epub' -o -iname '*.mobi' -o -iname '*.pdf' \\) -delete",
"test": "npm-run-all pretest test:unit pretest:acceptance test:acceptance",
"test:acceptance": "npm-run-all test:acceptance:*",
"test:acceptance:custom-fonts-ttf": "node ./src/index.js --debug --non-interactive --epub --pdf test/acceptance/custom-fonts-ttf && java -jar github_releases/w3c/epubcheck/epubcheck-*/epubcheck.jar test/acceptance/custom-fonts-ttf/*.epub",
"test:acceptance:custom-fonts-otf": "node ./src/index.js --debug --non-interactive --epub --pdf test/acceptance/custom-fonts-otf && java -jar github_releases/w3c/epubcheck/epubcheck-*/epubcheck.jar test/acceptance/custom-fonts-otf/*.epub",
Expand All @@ -56,6 +57,7 @@
"test:acceptance:metadata": "node ./src/index.js --debug --non-interactive --epub test/acceptance/metadata && java -jar github_releases/w3c/epubcheck/epubcheck-*/epubcheck.jar test/acceptance/metadata/*.epub",
"test:acceptance:toc": "node ./src/index.js --debug --non-interactive --epub --pdf test/acceptance/toc && java -jar github_releases/w3c/epubcheck/epubcheck-*/epubcheck.jar test/acceptance/toc/*.epub",
"test:acceptance:mobi": "node ./src/index.js --debug --non-interactive --mobi test/acceptance/latex-multi-files",
"test:acceptance:french": "node ./src/index.js --debug --non-interactive --epub --pdf test/acceptance/french && java -jar github_releases/w3c/epubcheck/epubcheck-*/epubcheck.jar test/acceptance/french/*.epub",
"test:unit": "mocha test/unit/**/*.test.js",
"build": "pkg . --out-path=build",
"start": "node ./src/index.js"
Expand Down
233 changes: 122 additions & 111 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const yaml = require('js-yaml')
const nodePandoc = require('./node-pandoc-promise')

const { latexHeader, latexNumber, latexImage, latexTOC, latexTitle, latexNewPage } = require('./helpers/latex')
const { defaultStylesheet, generateFontsStylesheet } = require('./helpers/stylesheet')
const { parseBool, generateFontFilename } = require('./utils')
const { defaultStylesheet, generateFontsStylesheet, getLanguageStylesheet } = require('./helpers/stylesheet')
const { generateFontFilename } = require('./helpers/fonts')
const { parseBool } = require('./utils')
const { epubToMobi } = require('./kindlegen')
const {
extract,
Expand Down Expand Up @@ -95,6 +96,7 @@ module.exports = async (config, options = {}) => {
;(config.styleSheets || []).forEach((filename) => {
cssContent += fs.readFileSync(path.join(cwd, filename), 'utf8')
})
cssContent += getLanguageStylesheet(config.metadata.lang)
cssContent += generateFontsStylesheet(config.fonts)
cssContent = new CleanCSS({
level: {
Expand All @@ -119,6 +121,11 @@ module.exports = async (config, options = {}) => {
}

if (config.format === 'pdf') {
if (config.metadata.lang) {
// Pass the lang option to the template
args.push('-V', `lang=${config.metadata.lang.split('-')[0]}`)
}

// use pdf engine
args.push('--pdf-engine=xelatex')

Expand Down
5 changes: 5 additions & 0 deletions src/helpers/fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports.generateFontFilename = (fonts = {}, type = 'main', shape = 'upright') => {
let filename = ((fonts[type].shapes || [])[shape] || '*').replace('*', fonts[type].baseFilename)
let extension = fonts[type].extension
return `${filename}${extension}`
}
18 changes: 18 additions & 0 deletions src/helpers/languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const En = require('./languages/en')
const Fr = require('./languages/fr')

const languages = [En, Fr]

module.exports.getLanguageData = (lang) => {
if (lang) {
let language = languages.find((l) => l.codes.includes(lang))
if (language) {
return language
}
language = languages.find((l) => l.codes.includes(lang.split('-')[0]))
if (language) {
return language
}
}
return En
}
9 changes: 9 additions & 0 deletions src/helpers/languages/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
codes: ['en'],
css: `section:not(.titlepage) p {
margin: 0;
}
section:not(.titlepage) p:not(:nth-child(2)) {
text-indent: 1.5em;
}`,
}
7 changes: 7 additions & 0 deletions src/helpers/languages/fr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
codes: ['fr'],
css: `section:not(.titlepage) p {
margin: 0;
text-indent: 1.5em;
}`,
}
9 changes: 5 additions & 4 deletions src/helpers/latex.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const defaultPackages = ['{tikz}', '{graphicx}', '{sectsty}']
const { cleanArray } = require('../utils')
const { generateFontFilename } = require('./fonts')

const { generateFontFilename } = require('../utils')
const defaultPackages = ['{tikz}', '{graphicx}', '{sectsty}']

module.exports.latexHeader = (latexPackages = [], fonts = {}) => {
let header = ''
let allPackages = defaultPackages.concat(latexPackages)
let allPackages = [...defaultPackages, ...latexPackages]
if (fonts.main) {
allPackages.push('{fontspec}')
}
allPackages.forEach((packageName) => {
cleanArray(allPackages).forEach((packageName) => {
header += `\\usepackage${packageName}\n`
})
if (fonts.main) {
Expand Down
13 changes: 7 additions & 6 deletions src/helpers/stylesheet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const path = require('path')
const { generateFontFilename } = require('../utils')

const { generateFontFilename } = require('./fonts')
const { getLanguageData } = require('./languages')

module.exports.defaultStylesheet = `body {
margin: 5%;
Expand Down Expand Up @@ -79,11 +81,6 @@ div.column {
div.hanging-indent {
margin-left: 1.5em;
text-indent: -1.5em;
}
section:not(.titlepage) p {
margin: 0;
text-indent: 1.5em;
}\n`

module.exports.generateFontsStylesheet = (fonts = {}) => {
Expand Down Expand Up @@ -113,3 +110,7 @@ module.exports.generateFontsStylesheet = (fonts = {}) => {

return result
}

module.exports.getLanguageStylesheet = (lang) => {
return `${getLanguageData(lang).css}\n`
}
6 changes: 1 addition & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,4 @@ module.exports.humanizePlatformArch = (platform, arch) => {
}
}

module.exports.generateFontFilename = (fonts = {}, type = 'main', shape = 'upright') => {
let filename = ((fonts[type].shapes || [])[shape] || '*').replace('*', fonts[type].baseFilename)
let extension = fonts[type].extension
return `${filename}${extension}`
}
module.exports.cleanArray = (array = []) => [...new Set(array)].filter(Boolean)
1 change: 0 additions & 1 deletion test/acceptance/custom-fonts-otf/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ default:
subtitle: EB Garamond
author: Guillaume Gérard
date: 2021-10
lang: fr-FR

textSubstitutions:
- regex: <p>\*{3}</p>
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/custom-fonts-ttf/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ default:
subtitle: Literata
author: Guillaume Gérard
date: 2021-10
lang: fr-FR

textSubstitutions:
- regex: <p>\*{3}</p>
Expand Down
17 changes: 17 additions & 0 deletions test/acceptance/french/reliure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
filename: My Ebook

default:
styleSheets:
- ../center.css
files:
- ../latex-one-file/ebook.tex

metadata:
title: Test - French
author: Guillaume Gérard
date: 2020-11
lang: fr-FR

textSubstitutions:
- regex: <p>\*{3}</p>
replacement: <p class="center">***</p>
1 change: 0 additions & 1 deletion test/acceptance/latex-multi-files/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ default:
subtitle: With multiple files
author: Guillaume Gérard
date: 2020-11
lang: fr-FR

extraMetadata:
'calibre:series': My Collection
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/latex-one-file/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ default:
subtitle: With one file
author: Guillaume Gérard
date: 2020-11
lang: fr-FR

extraMetadata:
'calibre:series': My Collection
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/markdown-multi-files/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ default:
subtitle: With multiple files
author: Guillaume Gérard
date: 2020-11
lang: fr-FR

extraMetadata:
'calibre:series': My Collection
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/markdown-one-file/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ default:
subtitle: With one file
author: Guillaume Gérard
date: 2020-11
lang: fr-FR

extraMetadata:
'calibre:series': My Collection
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/metadata/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ default:
- role: crr
text: contributor contributor 1
date: '2021-10-11'
lang: fr-FR
subject:
- subject 1
- subject 2
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/odt-one-file/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ default:
subtitle: With one file
author: Guillaume Gérard
date: 2020-11
lang: fr-FR

extraMetadata:
'calibre:series': My Collection
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/toc/reliure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ default:
title: Test - ToC
author: Guillaume Gérard
date: 2021-11
lang: fr-FR

pdf:
coverImage: ../cover-a4.jpg
Expand Down

0 comments on commit 2247a5b

Please sign in to comment.