marky
Quickstart Source2021-09-09
Abstract – This is the source code of the
marky
Example document for illustratingmarky
markup using simple examples. This document represents themarky
source code before processing python code embedded into the document itself. For documentation and download please refer to themarky
repository.
---
"`marky` Quickstart"
title:
author: lehmann72021-09-09
date: -includes--pdf:
header\usepackage{multicol}
\hypersetup{colorlinks=false,
={0 0 0},
allbordercolors={/S/U/W 1}}
pdfborderstyle-includes--html: >
header<style>* { box-sizing: border-box; }</style>
-cleveref: true
xnos-capitalise: true
xnos11pt
fontsize:
---
---
> **Abstract** -- This is a `marky` quickstart document for
> illustrating `marky` markup using simple examples.
> The `marky` source code of this document can be read
> [here](quick-src.html).
> For more information please refer to the
> [`marky` repository](https://github.com/lehmann7/marky),
> [`marky` documentation](marky.html) or the
> simple [`marky` example](example.html).
---
# Introduction
is a `marky` quickstart document for illustrating `marky` markup
This is the rendered version of
using simple examples. This document -src.html). The `marky` markup
the source code presented [here](quickis compatible with standard Markdown and can be read as-is.
This document represents the output of `marky` after processingis embedded into the document itself.
python code, which and see the complete
In order to understand the examples file can be read
`marky` syntax, the source code of this -src.html). The complete documentation of `marky`
[here](quickis available [here](marky.html)
---
# Markdown
is a Markdown preprocessor allowing to transform Markdown
`marky` is rendered to `pdf`
text using python. The preprocessed Markdown text and `html` (other formats using `pandoc`). `pandoc` has a powerful set
as well as
of Markdown extensions supporting structured writing -style
bibliography, figure referencing, table referencing, texwith referencing etc. (refer to Scientific Writing in
equations
Markdown, [`marky` Documentation](marky.html)).
and `pdf` consists of three
The rendering of Markdown text into `html`
steps which are illustrated using the `marky` documentation/marky.md`.
`md
1. *Preprocessing* \
(process `marky` markup, run code, generate content)* Input: `marky` Markdown text: `md/marky.md`
* Output: pandoc Markdown text: `build/marky.md`
2. *Linking* \
apply format specific code for `html` and `pdf`)
(* Input: pandoc Markdown text: `build/marky.md`
* Output
1. pandoc Markdown text for `html`: `build/marky.html.md`
2. pandoc Markdown text for `pdf`: `build/marky.pdf.md`
3. *Rendering* \
and `pdf` document using `pandoc`)
(render `html` * Input
1. pandoc Markdown text for `html`: `build/marky.html.md`
2. pandoc Markdown text for `pdf`: `build/marky.pdf.md`
* Output
1. `html` document: `html/marky.html`
2. `pdf` document: `pdf/marky.pdf`
is ecapsulated into a python script and a Makefile.
The whole process
Rendering documents using `marky` requires to write Markdown textand run `make all`.
---
# Automated Reporting
with embedded code snippets is a powerful paradigm for
Markdown text and scientific reporting and possibly other
automated technical
documents. On one hand data can be organized according to the documentin the report itself, and on the
structure using algorithms embedded in the report directly from
other hand the data can be inserted for manual copying of data into
variables. This elliminates the need and allows to update or reproduce the report automatically
the text for the same and other data. Using the simple `marky` syntax the user
from the Markdown perspective
can concentrate on documentation writing and assist the creation of document content using python code
snippets.
`pandoc` filters (refer to Related Work, [`marky` Documentation](marky.html))while rendering it. `pandoc` filters
allow transforming the document
operate on an internal abstract syntax tree (AST) representation,
therefore the user must express dynamically created document contentas nodes in the format of the AST. `marky` takes a slight different
and operates on the Markdown text itself, before it is parsed
approach and rendered.
is embedded into the document with a simple markup syntax
Python code //www.rmarkdown.org)
similar to [Rmarkdown](https:and inline expressions. `marky` parses the code,
using code blocks and writes the results back into the Markdown text. The
executes it or output formatted string
code can produce output using algorithms for the insertion of format dependent
variables. `marky` also allows in `html` and *tex* (for `pdf` documents).
raw code
1. code snippets are embedded into the document text
2. code snippets are executed during preprocessing
3. code produces text for the Markdown document
4. code variables are displayed in the document text
5. format dependent code is applied for `html` and `pdf`
---
# Download and Run `marky`
is Markdown preprocessor allowing to execute embedded python
`marky` in Markdown documents. After preprocessing, a regular Markdown
code file is present, which is rendered into `html` and `pdf` using
all this steps using a Makefile.
`pandoc`. `marky` handles is a single-file script which depends on `python` (>=3.6),
`marky` >=2.11), `pyyaml` and `pandoc-xnos`.
`pandoc` (
**Installing Dependencies**
for Debian-based Linux are released
`pandoc` binaries //github.com/jgm/pandoc/releases).
[here](https:is installed using the linux package manager or `pip` and
`pyyaml` -xnos` consists of the components `fignos`, `secnos`, `eqnos`
`pandocand `tablenos` which are installed using `pip`. Depending on the
linux installation maybe `pip3` has to be used.
```bash
pip install pyyaml-fignos
pip install pandoc-secnos
pip install pandoc-eqnos
pip install pandoc-tablenos
pip install pandoc
```
**Download `marky` Script**
is downloaded using the following commands.
`marky`
```bash
cd $HOME//github.com/lehmann7/marky.git
git clone https:
cd marky
```
Alternatively, marky can be obtained diretly without `git`:
```bash
cd $HOME
mkdir marky
cd marky//raw.githubusercontent.com/lehmann7/marky/main/marky.py
wget https:+x marky.py
chmod
```
**Initialize `marky` Environment**
and the documentation.
The `marky` environment consists of the Makefile and quickstart are unpacked from
The `marky` Makefile, documentation file into the current working directory.
the `marky.py` script is initialized using the following commands.
The `marky` environment
```bash
cd $HOME
cd marky/marky.py --init
./md/marky.md
WRITE ./md/marky.mdi
WRITE ./md/marky-src.md
WRITE ./md/quickstart.md
WRITE ./md/quick-src.md
WRITE ./md/example.md
WRITE ./md/example-src.md
WRITE ./data/marky.bib
WRITE .
USAGE1. `make help`
2. `make all-html httpd`
3. `make all-pdf`
```
/` and `data/`.
During initialization `marky` creates two directories `md/` is the directory which contains the Markdown text to be rendered
`mdand `pdf`. `data/` is the resource directory which contains
into `html` and other assets.
bibliography, images, videos
**Render Documentation and Examples**
all dependencies have been installed accordingly and the `marky`
If is initialized, `marky` can be used to render a local
environment and the example.
copy of the documentation, the quickstart
The following commands render the Markdown text of the documentation.
```bash
cd $HOME
cd markyall-pdf
make all-html
make
```
/` is created, which contains
During `make` a new directory `buildfor `html`
temporary files (preprocessed Markdown text, linked text and `pdf`). The resulting `html` and `pdf` documents are placed inside
/` and `pdf/`. For rendering `pdf` a tex environment like
`html
`texlive` needs to be installed. For rendering the `html` documents,and style
`pandoc` requires internet access, because java scripts from content delivery networks.
sheets are fetched
**`marky` Makefile**
The `marky` Makefile coordinates the three steps of the `marky`and rendering.
document processing pipeline: preprocessing, linking for displaying help
The `marky` Makefile supports several targets or rendering all, multiple or specific documents.
*Makefile Targets*
1. `make help`: display help message on the console
2. `make cheat`: display the `marky` markup Cheat Sheet
3. `make scan`: scan for new documents `md/*.md` and update Makefile
4. `make all`: render all documents `md/*.md` into `html` and `pdf`
5. `make all-pdf`: render all documents `md/*.md` into `pdf`
6. `make all-html`: render all documents `md/*.md` into `html`
7. `make httpd`: start python webserver in `html/`
8. `make clean`: remove all files: `build/*`, `pdf/*`, `html/*`
*Make Single Document*
all documents, which can
When running `make all`, `marky` renders if only one particular document shall be rendered.
be undesirable /*.md` for new Markdown
By `make scan`, `marky` scans the directory `md
documents to be processed. For each document, which has been found,in order to debug the preprocessing,
`marky` sets up alias targets and rendering of this document.
linking
/marky.md` shall be rendered step by step,
Assuming the document `md
`marky` introduces the following targets.
1. *Preprocessing*: `make md-marky` \
* `md/marky.md` -> `build/marky.md`
2. *Linking `html`*: `make lhtml-marky` \
* `build/marky.md` -> `build/marky.html.md`
3. *Linking `pdf`*: `make lpdf-marky` \
* `build/marky.md` -> `build/marky.pdf.md`
4. *Rendering `html`*: `make html-marky`
* `build/marky.html.md` -> `html/marky.html`
5. *Rendering `pdf`*: `make pdf-marky`
* `build/marky.pdf.md` -> `pdf/marky.pdf`
---
# Write A New Document
In order to render a new document the Markdown text needs to be savedfile located in `md/example.md` which can be found rendered
to a
[here](example.html). The following Markdown snippetas a starting point.
can be used
```yaml!!! example.md raw >>1
```
---
# Code Blocks
in Markdown using fenced code using
Code blocks are embedded \!` or `\!!` flag for displayed and hidden code
either the `
respectively.
```text\!
```
CODE_BLOCK_SHOWN
```
\!!
```
CODE_BLOCK_HIDDEN
```
```
**Display and Execute Code**
with `!` is displayed and executed.
This code block annotated
!
```import math
= math.sqrt(2)
x
```
**Execute Code without Display**
with `!` is not displayed, but executed.
The code block annotated
!!
```= x + 1
y
```
**Display Code but do not Execute**
is displayed as python, but not executed.
This code block
```python= 0./0.
z
```
**Using the Python `import` Statement**
from python modules and the
Large code blocks can be imported for loading installed libraries.
`import` statement can be used
```pythonimport numpy
import sys
".")
sys.path.append(#import module_in_working_directory
```
---
# Inline Code
Inline statements are directly embedded into the text flow usingand variables with the corresponding
expressions \!EXPRESSION` `` or `` `\!VARIABLE` ``.
syntax `` `
The output of variables can be formatted using the\!VARIABLE[:FORMAT]` `` or `` `\!EXPRESSION[:FORMAT]` ``
`` `<variable>[:<format>]}`
statement according to the python operator `{in the
implemented format()`](https://docs.python.org/3/library/string.html#formatstrings)
[`str.and the operator `{<expression>[:<format>]}` implemented in
specification -strings](https://docs.python.org/3/reference/lexical_analysis.html#f-strings).
[`f`
**Inline Formatted Output**
\sqrt{2}$ is:
The result of $* unformatted output: `! math.sqrt(2.0)`
* formatted output: `! "%.4f" % math.sqrt(2.0)`
is
The variable $x$ * unformatted output: `! x`
* formatted output: `! x:.4f`
=x+1$ is
The variable $y* unformatted output: `! y`
* formatted output: `! y:.4f`
for the definition of $y$ is hidden.)
(The code block
**Inline Expression Output**
!
```= list(range(1, 11))
x = [i*i for i in x]
y
def list_str(a):
return [str(i) for i in a]
def list_and(a):
return ", ".join(list_str(a[:-1])) + " and " + str(a[-1])
```
list can be inserted into the text. The square of the first
The !len(x)` numbers `!list_and(x)` is `!list_and(y)`. Square numbers
`=x^2$.
are computed according to $y
**Inline Statements in Tables**
!
```class square:
def __init__(self):
self.x = 0
def get_x(self):
return self.x
def next_y(self):
= self.x**2
y self.x += 1
return y
= square()
sq
```
is computed according to $y=x^2$.
The following table
|y
x -------------|--------------
!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`!sq.get_x()`|`!sq.next_y()`
`
---
# Generate Markdown Text
from a python algorithm
Markdown text can be produced algorithmically and `__()` function. The `_()` and `__()` function are
using the `_()`
special names which are reserved by `marky`. (refer to Generation offor in-depth
Markdown Text, [`marky` documentation](marky.html)
explanation).
## The `_()` Function
The `_()` function basicly resembled the python `print()` function.not patch the standard `print()` function which still
`marky` does in the console and not in the Markdown text.
displays text and the next
The `_()` function supports appending text to the previous as the fist or last parameter.
line of output by using `_` 1. `_(_, *args )`: append this output to previous output
2. `_(_, *args, _)`: append to previous and next output
3. `_( *args, _)`: append next output to this output
4. `_()`: disable append flag
**Monkey Patch `print()` Function**
*monkey patched* using the following
The `print()` function can be in order to call the `_()` function instead.
statment
!
```print("Print", "to", "console", "!")
"Print", "to", "Markdown", "!")
_(
print = _ # monkey patch
print("Print", "to", "Markdown", "!")
```
**Join Arguments using `sep`**
is `_(*args, sep=" ")`. `sep` is used to
The signature of the `_()` *args` into one string.
join the arguments `
!
```"The first five natural numbers are:")
_(1, 2, 3, 4, 5, sep=", ")
_(
```
**Generate a Table with Appending**
is used to create a table.
The append feature
!
```"Column 1", _)
_("|Column 2", _)
_("|Column 3", _)
_(
_()
"|".join(["--------"]*3))
_(
for i in range(5):
"% 8d" % (i*3))
_(for j in [1, 2]:
"|% 8d" % (i*3+j))
_(_,
```
The algorithm produces the following Markdown text.
```md1|Column 2|Column 3
Column --------|--------|--------
0| 1| 2
3| 4| 5
6| 7| 8
9| 10| 11
12| 13| 14
```
## The `__()` Function
Compared to the `_()` function, the `__()` function only takes oneand its purpose is to output a formatted paragraph with
argument is `_(arg, crop=True)`.
indentation. The signature of the `__()` function
**Generate a Paragraph with `f`-Strings**
with triple quoted block strings
The `__()` function can be combined and the python 3 `f`-strings or `f"..."` string interpolation, refer to
-strings](https://docs.python.org/3/reference/lexical_analysis.html#f-strings).
[`f`
!
```import random
= 0
s
random.seed(s)= [random.random() for i in range(3)]
p
f"""
__( Parameter one is {p[0]:.3f} and the value depends on the seed
of the pseudo random number generator, which was chosen
to be {s}. For the same seed always the same random numbers
are created. The next two numbers are {p[1]:.3f} and {p[2]:.3f}.
The sum of the three numbers is {sum(p):.3f} and it is
{'greater' if sum(p) > 2. else 'lesser or equal'} than two.
""")
```
**Cropping and Indentation of Output**
is printed into the
Before the text generated by the `__()` function is cropped according to the leading white space
document. The text -empty line. The leading white space of the first
of the first non-empty line is removed from all other lines of the output.
non
!
```"""
__( * List Level 1
* List Level 2
* List Level 2
* List Level 3
* List Level 2
* List Level 1
* List Level 1
""")
```
The code block produces the following output.
```md* List Level 1
* List Level 2
* List Level 2
* List Level 3
* List Level 2
* List Level 1
* List Level 1
```
**Disable Cropping of Output**
is disabled using the keyword `__(text, crop=False)`.
The cropping
!
```"""
__( * List Level 1
""", crop=True)
"""
__( * List Level 2
""", crop=False)
```
The code block produces the following output.
```md* List Level 1
* List Level 2
```
---
# Format Dependent Links
When writing several Markdown documents often documents are linked
between each other using the Markdown link statementfile.html)` or `[Link Name](file.pdf)`.
`[Link Name](with links into `html` and `pdf`
However, when rendering documents file extension often must be adjusted according to the output format.
the
`marky` supports the `.???` statement, which will be replaced byor `.pdf` depending on the output format.
`.html`
```mdfile.???)
[Link to document](
```
will be proprocessed into the following text:* for `html`: `[Link to document](file.html)`
* for `pdf`: `[Link to document](file.pdf)`
[Link to this document](quickstart.html)
---
# Format Dependent Code
format specific code within Markdown
`pandoc` Markdown allows to write and *tex* for `pdf` documents. However, when inserting
using `html` or raw *tex* code, the document only can be rendered into
raw `html` or `pdf` accordingly. This is only a short summary, for an
`html` in-depth explanation of all features please refer to Format Codes,
[`marky` documentation](marky.html).
format codes, which are applied during linking
`marky` introduces format specific codes for `html`
after preprocessing. During linking and `pdf` are applied in a consistent manner, resulting in documents
with Markdown and `html` or Markdown and *tex* only. Using this
pattern `marky` documents contain regular Markdown, which can beand `pdf`, as well as format specific codes
rendered into `html` for tweaking or polishing `html` and `pdf` output.
file `md/marky.md`, linking format codes
Assuming preprocessing the in the two following output files.
results 1. `build/marky.html.md`: contains output of `html` format codes.
2. `build/marky.pdf.md`: contains output of `pdf` format codes.
**Example 1: Multi-Column Text in `pdf` and `html`**
format codes `mcol_begin` and `mcol_end`, one for the
Defnition of two and another for the end of the multi column
begin of multi column format codes are appended with `_html` and `_pdf`
section. The
respectively.
!
```= fmtcode(
mcol_begin =r"\begin{multicols}{2}",
pdf="<div style='column-count: 2;'>"
html
)= fmtcode(
mcol_end =r"\end{multicols}",
pdf="</div>"
html
)
```
-count` CSS property requires Internet Explorer>=10,
The `column>=52, Safari>=9, Opera>=37 or Chrome>=50, refer to
Firefox//www.w3schools.com/css/css3_multiple_columns.asp).
[w3schools](https:*tex* package, the statement
In order to use the `multicol` \usepackage{multicol}` has to be included in the yaml meta data
`in the front matter of the Markdown document.
!mcol_begin()`
`
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text!mcol_end()`
`
---
# Include Statement
\!!!` statement.
`marky` allows to include other Markdown text using the `for complete description
Please refer to the `marky` documentation \!!!` statement. During rendering `marky` keeps track of
of the `and creates Makefile rules for dependent make.
included files
```md!!! file.mdi
```
---
# Meta Data
in Markdown front matter.
`marky` supports document meta data is not explained in the quickstart. Please refer
This feature for explanation.
to the `marky` documentation
```md---
META_DATA---
MARKDOWN
```
---
# Escape Markup
is escaped
The `marky` markup can be escaped. When markup and prints out the
`marky` removes the escape sequence
unescaped statement.
|Escape Sequence|Unsecaped Sequence
Markup -----------------|---------------|-----------------
|`` ```\\!! `` |`` ```\!! ``
code block hidden|`` ```\\! `` |`` ```\! ``
code block shown |`` `\\!...` `` |`` `\!...` ``
inline code format code |`` `\\?...` `` |`` `\?...` ``
|`\\!!!` |`\!!!`
include statementformat link |`.\???` |`.???`
---
*Thanks for reading, please try `marky`.*