A tiny web type setter
Project description
degrotesque — A tiny web type setter.
Introduction
The script loads an HTML page — or several in batch, one after the other — and for each, it replaces some commonly used non-typographic characters, such as ", ', -, etc. into their typographic representant for improving the pages' appearance.
E.g.:
"Well - that's not what I had expected."
will become:
“Well — that's not what I had expected.”
(Uhm, uhm, for those who don't see it, the starting and ending quotes have been replaced by “ and ”, respectively, the ' by ' and the - by an —.)
Download and Installation
The current version is degrotesque-1.6.
You may install degrotesque using
python -m pip install degrotesque
You may download a copy or fork the code at degrotesque's github page.
Besides, you may download the current release degrotesque-1.6 here:
License
degrotesque is licensed under LGPL 3.0.
Documentation
Usage
degrotesque is implemented in Python. It is started on the command line.
The option -i <PATH> / --input <PATH> tells the script which file(s) shall be read — you may name a file or a folder, here. If the option -r / --recursive is set, the given folder will be processed recursively.
The tool processes HTML files, XML files, and their derivatives. The extensions of file types that are processed are given in Appendix A. You may change the extensions of files to process using the -e <EXTENSION>[,<EXTENSION>]* / --extensions <EXTENSION>[,<EXTENSION>]* option.
The files are read one by one and the replacement of plain ASCII chars by some nicer ones is based upon a chosen set of “actions”. Known and default actions are given in Appendix B. You may select the actions to apply using the -a <ACTION_NAME>[,<ACTION_NAME>]* / --actions <ACTION_NAME>[,<ACTION_NAME>]* option. The default actions are masks, quotes.english, dashes, ellipsis, math, and apostrophe. Per default, HTML entities are inserted. If you rather wish to have unicode values, use the option -u / --unicode.
The files are assumed to be encoded using UTF-8 per default. You may change the encoding using the option -E <ENCODING> / --encoding <ENCODING>.
The script does not change the quotation marks of HTML elements, of course. As well, the contents of several elements, such as <code> or <pre>, are skipped. You may change the list of elements which contents shall not be processed using the option -s <ELEMENT_NAME>[,<ELEMENT_NAME>]* / --skip <ELEMENT_NAME>[,<ELEMENT_NAME>]*. The list of elements that are skipped per default is given in Appendix C.
After the actions have been applied to its contents, the file is saved. By default, a backup of the original file is saved under the same name, with the appendix “.orig”. You may omit the creation of these backup files using the option -B / --no-backup.
Please note that “masks” is a special action set that disallows the application of some other actions so that, e.g., the dividers in ISBN numbers are not replaced by –. The masks action set is given in Appendix D.
Options
The script has the following options:
- --input/-i <PATH>: the file or the folder to process
- --recursive/-r: Set if the folder — if given — shall be processed recursively
- --no-backup/-B: Set if no backup files shall be generated
- --unicode/-u: When set, unicode characters instead of HTML-entities are used
- --extensions/-e <EXTENSION>[,<EXTENSION>]*: The extensions of files that shall be processed
- --encoding/-E <ENCODING>: The assumed encoding of the files
- --skip/-s <ELEMENT_NAME>[,<ELEMENT_NAME>]*: Elements which contents shall not be changed
- --actions/-a <ACTION_NAME>[,<ACTION_NAME>]*: Name the actions that shall be applied
- --help: Prints the help screen
Examples
degrotesque -i my_page.html -a quotes.german
Replaces single and double quotes within the file "my_page.html" by their typographic German counterparts.
degrotesque -i my_folder -r --no-backup
Applies the default actions to all files that match the extension in the folder "my_folder" and all subfolders. No backup files are generated.
Application Programming Interface - API
You may as well embedd degrotesque within your own applications. The usage is very straightforward:
import degrotesque
# build the degrotesque instance with default values
degrotesque = degrotesque.Degrotesque()
# apply degroteque
prettyString = degrotesque.prettify(uglyString)
The default values can be replaced using some of the class' interfaces (methods):
# change the actions to apply (by naming them)
# here: apply french quotes and math symbols
degrotesque.setActions("quotes.french,math")
# change the elements which contents shall be skipped
# here: skip the contents of "code",
# "script", and "style" elements
degrotesque.setToSkip("code,script,style")
You may as well consult the degrotesque pydoc code documentation.
Further Documentation
- The web page is located at: http://www.krajzewicz.de/blog/degrotesque.php
- The PyPI page is located at: https://pypi.org/project/degrotesque/
- The github repository is located at: https://github.com/dkrajzew/degrotesque
- The issue tracker is located at: https://github.com/dkrajzew/degrotesque/issues
- The Travis CI page is located at: https://travis-ci.com/github/dkrajzew/degrotesque
- The code documentation (pydoc) is located at: http://www.krajzewicz.de/blog/degrotesque.html
Implementation Notes
- I tried Genshi, BeautifulSoup, and lxml. All missed in keeping the code unchanged. So the parser just skips HTML-elements and the contents of some special elements, see above. Works in most cases.
Examples / Users
- My own pages (http://www.krajzewicz.de/).
Change Log
Version 1.6
- reworked tests, now using pytest and unittest
- issue #10: will not use TextTest here; using pytest instead
- issue #11: using coverage.py instead of coveralls
- added the -u/--unicode option which forces to use unicode codes instead of HTML entities
Older Versions
- see ChangeLog
Summary
Well, have fun. If you have any comments / ideas / issues, please submit them to degrotesque's issue tracker on github.
Appendices
Appendix A: Default Extensions
Files with the following extensions are parsed per default:
- html, htm, xhtml,
- php, phtml, phtm, php2, php3, php4, php5,
- asp,
- jsp, jspx,
- shtml, shtm, sht, stm,
- vbhtml,
- ppthtml,
- ssp, jhtml
Appendix B: Named Actions
The following action sets are currently implemented.
Please note that the actions are realized using regular expressions. I decided not to show them in the following for a better readability and show the visible changes only.
Action Name | From Opening String | From Closing String | To Opening String | To Closing String |
---|---|---|---|---|
quotes.english | ' | ' | ‘ | ’ |
" | " | “ | ” | |
quotes.french | < | > | ‹ | › |
<< | >> | « | » | |
quotes.german | ' | ' | ‚ | ’ |
" | " | „ | ” | |
to_quotes | ' | ' | <q> | </q> |
" | " | <q> | </q> | |
<< | >> | <q> | </q> | |
< | > | <q> | </q> | |
commercial | (c) | © | ||
(r) | ® | |||
(tm) | ™ | |||
dashes | - | — | ||
<NUMBER>-<NUMBER> | <NUMBER>–<NUMBER> | |||
bullets | * | • | ||
ellipsis | ... | … | ||
apostrophe | ' | ' | ||
math | +/- | ± | ||
1/2 | ½ | |||
1/4 | ¼ | |||
3/4 | ¾ | |||
~ | ≈ | |||
!= | ≠ | |||
<= | ≤ | |||
>= | ≥ | |||
<NUMBER>*<NUMBER> | <NUMBER>×<NUMBER> | |||
<NUMBER>x<NUMBER> | <NUMBER>×<NUMBER> | |||
<NUMBER>/<NUMBER> | <NUMBER>÷<NUMBER> | |||
dagger | ** | ‡ | ||
* | † |
Appendix C: Skipped Elements
The contents of the following elements are not processed by default:
- script
- code
- style
- pre
- ?
- ?php
- %
- %=
- %@
- %--
- %!
- !--
Appendix D: Masking Action Set
The “masks” action set is masking some patterns to avoid replacements. When matching, the matching string is kept. The actions are given in the following. Please note that the numbers in { } brackets give the number of subsequent elements.
- 978-<NUMBER>-<NUMBER>-<NUMBER>-<NUMBER>{1}<NO_NUMBER>: avoid ISBN replacement
- 979-<NUMBER>-<NUMBER>-<NUMBER>-<NUMBER>{1}<NO_NUMBER>: avoid ISBN replacement
- <NUMBER>-<NUMBER>-<NUMBER>-<NUMBER>{1}<NO_NUMBER>: avoid ISBN replacement
- ISSN <NUMBER>{4}-<NUMBER>{4}: avoid ISSN replacement
© Daniel Krajzewicz 2020–2022
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file degrotesque-1.6.tar.gz
.
File metadata
- Download URL: degrotesque-1.6.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f2d95d7ead1aa94a700d2acbaa4acf46993aacbf228413b19a40edb78e01e77 |
|
MD5 | 61399d0206bb2ae16a16758d4a6fa512 |
|
BLAKE2b-256 | 7edb90a497590138c13f5ff5e8bf24491e39808457ed3acd03f7653e2c02fc29 |
File details
Details for the file degrotesque-1.6-py3-none-any.whl
.
File metadata
- Download URL: degrotesque-1.6-py3-none-any.whl
- Upload date:
- Size: 21.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2164f0067a37211ef1fe4765c66c484f6b20ddcb608a7d969b8e91195cf499a |
|
MD5 | 9f1d86e3e9058efcc63e4fbd41e996dc |
|
BLAKE2b-256 | 8d26c18236ad7af05ef042c54ff7cec61d66cedfcdc6dc4ea1acdb9b97dc79f4 |