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 21, 2024
1 parent 72d80c3 commit 4ecbf52
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 30 deletions.
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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
8 changes: 5 additions & 3 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 Down Expand Up @@ -123,7 +125,7 @@ module.exports = async (config, options = {}) => {
args.push('--pdf-engine=xelatex')

// add nice chapter split for pdf
let headerContent = latexHeader(config.latexPackages, config.fonts)
let headerContent = latexHeader(config.latexPackages, config.fonts, config.metadata.lang)
let headerFile = path.join(tempPath, 'header.tex')
fs.writeFileSync(headerFile, headerContent, 'utf8')
args.push('--include-in-header', headerFile)
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}`
}
17 changes: 17 additions & 0 deletions src/helpers/languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const languages = require('./languages/index')

const DEFAULT_LANGUAGE = 'en'

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 languages.find((l) => l.codes.includes(DEFAULT_LANGUAGE))
}
10 changes: 10 additions & 0 deletions src/helpers/languages/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
codes: ['en'],
latexPackages: [],
css: `section:not(.titlepage) p {
margin: 0;
}
section:not(.titlepage) p:not(:nth-child(2)) {
text-indent: 1.5em;
}`,
}
8 changes: 8 additions & 0 deletions src/helpers/languages/fr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
codes: ['fr'],
latexPackages: ['[french]{babel}'],
css: `section:not(.titlepage) p {
margin: 0;
text-indent: 1.5em;
}`,
}
4 changes: 4 additions & 0 deletions src/helpers/languages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const En = require('./en')
const Fr = require('./fr')

module.exports = [En, Fr]
12 changes: 7 additions & 5 deletions src/helpers/latex.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const defaultPackages = ['{tikz}', '{graphicx}', '{sectsty}']
const { cleanArray } = require('../utils')
const { generateFontFilename } = require('./fonts')
const { getLanguageData } = require('./languages')

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

module.exports.latexHeader = (latexPackages = [], fonts = {}) => {
module.exports.latexHeader = (latexPackages = [], fonts = {}, lang) => {
let header = ''
let allPackages = defaultPackages.concat(latexPackages)
let allPackages = [...defaultPackages, ...getLanguageData(lang).latexPackages, ...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
1 change: 0 additions & 1 deletion test/acceptance/docx-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
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 4ecbf52

Please sign in to comment.