Skip to content

Commit

Permalink
feat: make a minimal cli wrapper (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krande committed Dec 19, 2023
1 parent 47e2a5b commit dddd804
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 25 deletions.
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,42 @@ variable substitution. Use `{{__variable__}}` in your markdown files to insert a
Install using

```
conda install -c krande -c conda-forge paradoc
mamba install -c krande -c conda-forge paradoc
```

Note! This is a very experimental project so expect things to break.


## Usage

Assuming you have a folder structure set up as this:
Paradoc by default assumes you have a folder structure set up as this;

```mermaid
graph LR
. --> 00-main/
. --> 01-app/
. --> build.py
```

whereas `00-main` and `01-app` contains markdown (.md) files representing content in your
main and appendix respectively. Both `00-main` and `01-app` can contain as many subdirectories
as you prefer. The document order is simply the alphabetical order of subdirectories and naming of markdown files.

### Convert to a single Word DOCX
### Cli usage

Make sure you activate the conda environment you installed `paradoc` in. Then run

```bash
paradoc . "my-doc" --auto-open
```

_(for now the variable substitution scheme is not implemented in the cli)_

Use `paradoc --help` to see all available options.

### Usage as a python module

This is for more advanced users who want to integrate paradoc into their own build system.

#### Convert to a single Word DOCX

```python
# build.py
Expand All @@ -42,7 +57,9 @@ one = OneDoc()
one.compile("MyDocument.docx")
```

### Performing variable substitution
_(this is pretty much what the cli does under the hood)_

#### Performing variable substitution

Assuming you have somewhere in any of your documents a variable `{{__val_gM__}}` the following
example will substitute that variable with the number 1.15.
Expand Down Expand Up @@ -73,9 +90,6 @@ flake8 .
black .
````

Or if you have make installed you can just run `make format`
to run all three tools at once.

## Project Responsible ###

Kristoffer H. Andersen
2 changes: 1 addition & 1 deletion action_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ enabled = true
pre_release_tag = "dev"

[tool.python.pip]
enabled = true
enabled = false

[tool.python.conda]
enabled = true
Expand Down
5 changes: 3 additions & 2 deletions conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ source:
build:
noarch: python
number: 0
script: "{{ PYTHON }} -m pip install . -vv"
script: python -m pip install . --no-deps -vv
entry_points:
- paradoc=paradoc.cli_app:app

requirements:
build:
host:
- python
run:
- python
Expand All @@ -34,6 +34,7 @@ requirements:
- pytexit
- typer
- nomkl
- pywin32 # [win]
test:
source_files:
- tests
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
"Topic :: Utilities",
]
[project.scripts]
ada-viewer = "paradoc.cli_app:app"
paradoc = "paradoc.cli_app:app"

[project.urls]
"Homepage" = "https://github.com/Krande/paradoc"
Expand Down
22 changes: 22 additions & 0 deletions src/paradoc/cli_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import typer

from paradoc import OneDoc
from paradoc.common import ExportFormats

app = typer.Typer()


@app.command("paradoc")
def main(
source_dir: str,
report_name: str,
auto_open: bool = False,
work_dir: str = None,
export_format: ExportFormats = ExportFormats.DOCX,
):
one = OneDoc(source_dir, work_dir=work_dir)
one.compile(report_name, auto_open=auto_open, export_format=export_format)


if __name__ == "__main__":
app()
3 changes: 2 additions & 1 deletion src/paradoc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pathlib
import re
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Union

import pandas as pd
Expand Down Expand Up @@ -103,7 +104,7 @@ def get_figures(self):
return regx.finditer(self.read_original_file())


class ExportFormats:
class ExportFormats(str, Enum):
DOCX = "docx"
PDF = "pdf"
HTML = "html"
7 changes: 5 additions & 2 deletions src/paradoc/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def __init__(
clean_build_dir=True,
create_dirs=False,
output_dir=None,
work_dir=None,
**kwargs,
):
self.source_dir = pathlib.Path().resolve().absolute() if source_dir is None else pathlib.Path(source_dir)
self.work_dir = kwargs.get("work_dir", pathlib.Path("").resolve().absolute())
self.work_dir = pathlib.Path(work_dir) if work_dir is not None else pathlib.Path("")
self.work_dir = self.work_dir.resolve().absolute()

self._main_prefix = main_prefix
self._app_prefix = app_prefix
self._output_dir = output_dir
Expand Down Expand Up @@ -127,7 +130,7 @@ def _setup(self, create_dirs, app_prefix, clean_build_dir):
shutil.rmtree(self.build_dir, ignore_errors=True)

def compile(self, output_name, auto_open=False, metadata_file=None, export_format=ExportFormats.DOCX, **kwargs):
dest_file = (self.dist_dir / output_name).with_suffix(f".{export_format}").resolve().absolute()
dest_file = (self.dist_dir / output_name).with_suffix(f".{export_format.value}").resolve().absolute()

print(f'Compiling OneDoc report to "{dest_file}"')
os.makedirs(self.build_dir, exist_ok=True)
Expand Down
2 changes: 0 additions & 2 deletions src/paradoc/io/html/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def export(self, dest_file):
# f"--reference-doc={MY_DOCX_TMPL}",
],
filters=["pandoc-crossref"],
encoding="utf8",
)
print(f'Successfully exported HTML to "{dest_file}"')

Expand All @@ -46,6 +45,5 @@ def docx2pdf(docx_file, output_file):
"pdf",
extra_args=["--pdf-engine=pdflatex"],
outputfile=str(output_file),
encoding="utf8",
sandbox=False,
)
2 changes: 0 additions & 2 deletions src/paradoc/io/pdf/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def export(self, dest_file):
# f"--reference-doc={MY_DOCX_TMPL}",
],
filters=["pandoc-crossref"],
encoding="utf8",
)
print(f'Successfully exported PDF to "{dest_file}"')

Expand All @@ -41,6 +40,5 @@ def docx2pdf(docx_file, output_file):
"pdf",
extra_args=["--pdf-engine=pdflatex"],
outputfile=str(output_file),
encoding="utf8",
sandbox=False,
)
7 changes: 4 additions & 3 deletions src/paradoc/io/word/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def _compile_individual_md_files_to_docx(self, output_name, dest_file):
# f"--reference-doc={MY_DOCX_TMPL}",
],
filters=["pandoc-crossref"],
encoding="utf8",
sandbox=False,
)

Expand Down Expand Up @@ -87,7 +86,10 @@ def _compile_individual_md_files_to_docx(self, output_name, dest_file):

def format_tables(self, composer_doc: Document, is_appendix):
for i, docx_tbl in enumerate(self.get_all_tables(composer_doc)):
cell0 = docx_tbl.get_content_cell0_pg()
try:
cell0 = docx_tbl.get_content_cell0_pg()
except IndexError:
continue
tbl_name = cell0.text
tbl = self.one_doc.tables.get(tbl_name, None)
if tbl is None:
Expand Down Expand Up @@ -163,5 +165,4 @@ def _compile_docx_from_str(self, dest_file):
# f"--reference-doc={MY_DOCX_TMPL}",
],
filters=["pandoc-crossref"],
encoding="utf8",
)
1 change: 0 additions & 1 deletion src/paradoc/io/word/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ def convert_markdown_dir_to_docx(source, dest, dest_format, extra_args, style_do
outputfile=str(new_file),
extra_args=extra_args,
filters=["pandoc-crossref"],
encoding="utf8",
sandbox=False,
)
logger.info(output)
Expand Down
1 change: 0 additions & 1 deletion src/paradoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def convert_markdown(
outputfile=str(dest),
extra_args=extra_args,
filters=["pandoc-crossref"],
encoding="utf8",
sandbox=False,
)
logger.info(output)
Expand Down

0 comments on commit dddd804

Please sign in to comment.