gummy.utils.outfmt_utils module¶
Utility programs for creating HTML or PDF.
-
gummy.utils.outfmt_utils.
sanitize_filename
(fp, dirname=None, ext=None, allow_unicode=False)[source]¶ Convert from original filename to sanitized filename
- Parameters
fp (str) – File path.
dirname (str) – Directory part of the
fp
ext (str) – Required file extension.
allow_unicode (bool) – Whether allowing unicode or not.
- Returns
Sanitized filename.
- Return type
str
Examples
>>> from gummy.utils import sanitize_filename >>> sanitize_filename("path/to/image–.png") >>> 'path/to/image.png' >>> sanitize_filename("path/to/image–.jpg", ext=".png") >>> 'path/to/image.jpg.png' >>> sanitize_filename("path/to/image–.png", allow_unicode=True) >>> 'path/to/image–.png' >>> # Doesn't work >>> sanitize_filename(fp='mir-193 targets ALDH2 and contributes to toxic aldehyde accumulation and tyrosine hydroxylase dysfunction in cerebral ischemia/reperfusion injury') 'mir-193 targets ALDH2 and contributes to toxic aldehyde accumulation and tyrosine hydroxylase dysfunction in cerebral ischemia/reperfusion injury' >>> # Work well :) >>> sanitize_filename(fp='mir-193 targets ALDH2 and contributes to toxic aldehyde accumulation and tyrosine hydroxylase dysfunction in cerebral ischemia/reperfusion injury', dirname=".") './mir-193 targets ALDH2 and contributes to toxic aldehyde accumulation and tyrosine hydroxylase dysfunction in cerebral ischemia0reperfusion injury'
-
gummy.utils.outfmt_utils.
get_jinja_all_attrs
(string, keyname)[source]¶ Get the keynames which each element in
keyname
is expected to have.- Parameters
string (str) – Content strings in template file.
keyname (str) – keyname which is used in
template.render(keyname=...)
Examples
>>> import os >>> from gummy.utils import TEMPLATES_DIR, get_jinja_all_attrs >>> path = os.path.join(TEMPLATES_DIR, "paper.html") >>> with open(path, mode="r", encoding="utf-8") as f: ... html = "".join(f.readlines()) >>> get_jinja_all_attrs(string=html, keyname="contents") {'en', 'head', 'img', 'ja', 'subhead'}
-
gummy.utils.outfmt_utils.
check_contents
(path, contents)[source]¶ Check whether all attributes in template is contained in contents.
- Parameters
path (str) – path/to/template.file
contens (list) – Each element in
contents
should be dictionary.
-
gummy.utils.outfmt_utils.
tohtml
(path, title='', contents=[], searchpath='/Users/iwasakishuto/Github/portfolio/Translation-Gummy/gummy/templates', template='paper.html', verbose=True)[source]¶ Arrange
title
andcontents
in html format.- Parameters
path (str) – path/to/output.html
title (str) – title for html.
contents (list) – Contens which used for render method of
jinja2.environment.Template
instance.searchpath (str) – Loader will find templates from the file system, and this directory is a base.
template (str) – template filename. Loader will find
f"{searchpath}/{template}"
- Returns
path/to/output.html
- Return type
str
-
gummy.utils.outfmt_utils.
html2pdf
(path, delete_html=True, verbose=True, options={})[source]¶ Convert from HTML to PDF.
- Parameters
path (str) – path/to/input.html
delete_html (bool) – Whether you want to delete html file. (default=
True
)verbose (bool) – Whether to print message or not. (default=
True
)options (dict) – options for wkhtmltopdf. See https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
- Returns
path/to/output.pdf
- Return type
str
-
gummy.utils.outfmt_utils.
toPDF
(path, title='', contents=[], searchpath='/Users/iwasakishuto/Github/portfolio/Translation-Gummy/gummy/templates', template='paper.html', verbose=True, options={})[source]¶ Arrange
title
andcontents
in html format, then convert it to PDF.- Parameters
path (str) – path/to/output.html
title (str) – title for html.
contents (list) – Contens which used for render method of
jinja2.environment.Template
instance.searchpath (str) – Loader will find templates from the file system, and this directory is a base.
template (str) – template filename. Loader will find
f"{searchpath}/{template}"
verbose (bool) – Whether to print message or not. (default=
True
)options (dict) – options for wkhtmltopdf. See https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
- Returns
path/to/output.pdf
- Return type
str