Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[codz]
*$py.class

# C extensions
*.so

# Venv stuff
bin/
include/
pyvenv.cfg

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
lib64
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
out/
*.epub

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py.cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
#poetry.toml

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
#pdm.lock
#pdm.toml
.pdm-python
.pdm-build/

# pixi
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
#pixi.lock
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
# in the .venv directory. It is recommended not to include this directory in version control.
.pixi

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.envrc
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the entire vscode folder
.vscode/

# PyPI configuration file
.pypirc
58 changes: 47 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,33 @@ mark2epub requires:

- Python (>= 3.4)
- markdown (>= 3.1)
- BeautifulSoup4 (>= 4.14.3)
- lxml (>= 6.0.2)

### Running mark2epub

The syntax for mark2epub is the following:

$ python md2epub.py <markdown_directory> <output_file.epub>

The output file path can contain unexisting directories, as the code will handle their creation.

The directory `epub_md` is a sample markdown directory for mark2epub.

Note that the directory `markdown_directory` **must** contain

* Markdown `.md` files. Each file represent a chapter in the resulting ePub.
They are processed by name order, and will appear correspondingly in the e-book.
- Markdown `.md` files. Each file represent a chapter in the resulting ePub.
They are processed by name order, and will appear correspondingly in the e-book.

* An `images` folder, containing the images to be included. Only GIF (`.gif`
- An `images` folder, containing the images to be included. Only GIF (`.gif`
extension), JPEG (`.jpg` or `.jpeg` extensions), and PNG (`.png` extension)
files are currently supported. This folder is *not* processed recursively, so
files are currently supported. This folder is _not_ processed recursively, so
all images should be placed at the root of this folder.

* A `css` folder, containing the CSS files. This folder is *not* processed
recursively, so all css files should be placed at the root of this folder.
- A `css` folder, containing the CSS files. This folder is _not_ processed
recursively, so all css files should be placed at the root of this folder.

* A `description.json` containing meta-information about the e-book. The key
- A `description.json` containing meta-information about the e-book. The key
`cover_image` should indicate the name of the cover image.
The key `default_css` is a list of css file names that are applied by default
on all chapters.
Expand All @@ -42,9 +46,41 @@ They are processed by name order, and will appear correspondingly in the e-book.
the name of the css file that should be applied specifically to this chapter.
See the example in the repository for a typical `description.json` file.

## Specific class names for elements

After your text, on every line (at the end of it) you can add class names to that element, as a list separated by whitespaces, included in curly braces, as follows:

```markdown
# Chapter 1 {name1 name-2 names-of-names etc}
```

Class names can consist of latin letters, digits, hyphens and underscores.

After compiling the braces and their insides will be removed.

The code won't handle any typos in class names, so it's up to you to handle it.

## Obsidian tags

You can safely use obsidian tags in your markdown files, as the preprocessor will handle their exclusion from the final built epub.

You can also specify which style of new line you use (before the tag, after it, or without any newline before or after the tag) in the description.json, `tag_new_line_style` field. This new line will be excluded along with the tag.

The valid values are: `before`, `after` and `zero`, respectively, in upper, lower or mixed case (you do you!).

## No cover

To not set any cover, simply leave the `"cover_image": ""`.

## Limitations/Features to be addressed

* Robustness checks in the `mark2epub.py` script
* Recursive processing of the `images` and `css` folders
* Support for additional fonts
* Support for mathematical notation
- Robustness checks in the `mark2epub.py` script
- Recursive processing of the `images` and `css` folders
- Support for additional fonts
- Support for mathematical notation

## TODO

- [ ] Error handling for class names parsing
- [x] Add support for symlinks
- [x] Add support for obsidian tags
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/bash

source ./bin/activate
pip install -r requirements.txt
pyinstaller --clean -F -y -n "mark2epub" mark2epub.py
30 changes: 16 additions & 14 deletions epub_md/chapter1.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Chapter 1
# Chapter 1 {example of multiple class names}

This is some simple text in Markdown.
This is some simple text in Markdown. {WORKS ON EVERY TAG ALLEGEDLY THO}

## Section 1
#example_of_an_obsidian_tag #multiple_tags

## Section 1 {Allows only whitelisted characters google it}

Denique Antiochensis **ordinis vertices sub uno elogio iussit occidi** ideo efferatus,
quod ei celebrari vilitatem intempestivam urgenti, cum inpenderet inopia,
Expand All @@ -11,7 +13,7 @@ Honoratus fixa constantia restitisset.

## Section 2

Et interdum acciderat, *ut siquid in penetrali secreto nullo citerioris vitae*
Et interdum acciderat, _ut siquid in penetrali secreto nullo citerioris vitae_
ministro praesente paterfamilias uxori susurrasset in aurem, velut Amphiarao
referente aut Marcio, quondam vatibus inclitis, postridie disceret imperator.
ideoque etiam parietes arcanorum soli conscii timebantur.
Expand All @@ -20,21 +22,21 @@ ideoque etiam parietes arcanorum soli conscii timebantur.

Saepissime igitur mihi de amicitia cogitanti maxime illud considerandum
videri solet, utrum propter imbecillitatem atque inopiam desiderata sit amicitia,
ut dandis recipiendisque meritis
ut dandis recipiendisque meritis

* quod quisque minus per se ipse posset,
* id acciperet
* ab alio vicissimque redderet,
* an esset hoc quidem proprium amicitiae,
*sed antiquior et
* pulchrior et magis a natura ipsa profecta alia causa.
- quod quisque minus per se ipse posset,
- id acciperet
- ab alio vicissimque redderet,
- an esset hoc quidem proprium amicitiae,
\*sed antiquior et
- pulchrior et magis a natura ipsa profecta alia causa.

### Subsection

Amor enim, ex quo amicitia nominata est,
princeps est ad benevolentiam coniungendam.

1. Nam utilitates quidem etiam ab iis percipiuntur saepe
1. qui simulatione amicitiae coluntur
2. et observantur temporis causa,
2. in amicitia autem nihil fictum est, nihil simulatum et, quidquid est, id est verum et voluntarium.
1. qui simulatione amicitiae coluntur
2. et observantur temporis causa,
2. in amicitia autem nihil fictum est, nihil simulatum et, quidquid est, id est verum et voluntarium.
19 changes: 12 additions & 7 deletions epub_md/css/general.css
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
body {
font-size: 0.8em;
font-size: 0.8em;
}

.example {
color: red;
}

img {
max-width: 100%;
height: auto;
max-width: 100%;
height: auto;
}


table {
border-collapse: collapse;
width: 100%;
font-size: 0.5em;
}

td,th {
td,
th {
border: 1px solid #ddd;
padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2;}
tr:nth-child(even) {
background-color: #f2f2f2;
}

th {
padding-top: 12px;
Expand All @@ -29,4 +34,4 @@ th {
background-color: #444444;
color: white;
font-size: 0.8em;
}
}
Loading