Skip to content

Commit

Permalink
Merge pull request #13 from databio/dev
Browse files Browse the repository at this point in the history
v1
  • Loading branch information
nsheff authored Nov 22, 2022
2 parents 97c0530 + 62e5913 commit 72cede9
Show file tree
Hide file tree
Showing 42 changed files with 912 additions and 536 deletions.
26 changes: 0 additions & 26 deletions demo/md_template.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,3 @@ This is a jinja template. You can write plain markdown text into the template li

## YAML data

You can also refer to variables in the yaml files, using syntax like this:

{% raw %}
{{ number_of_things }}
{{ name_of_person }}
{% endraw %}

Which renders like this:

{{ number_of_things }}
{{ name_of_person }}

## Markdown data

You can refer to *content* in the markdown data with the `.content` attribute, using the name you put in the config file {% raw %}{{ some_text_data.content }}{% endraw %}, which renders like this:

```
{{ some_text_data.content }}
```


You can also refer to the yaml metadata in your markdown files, using {% raw %}{{ some_text_data.metadata.text_property }}{% endraw %}:

```
{{ some_text_data.metadata.text_property }}
```
12 changes: 5 additions & 7 deletions demo/rendered.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This is a jinja template. You can write plain markdown text into the template li
You can also refer to variables in the yaml files, using syntax like this:


15
The sundance kid
{{ number_of_things }}
{{ name_of_person }}


Which renders like this:
Expand All @@ -18,9 +18,7 @@ The sundance kid

## Markdown data

You can refer to *content* in the markdown data with the `.content` attribute, using the name you put in the config file # Text content

Here is some text, which renders like this:
You can refer to *content* in the markdown data with the `.content` attribute, using the name you put in the config file {{ some_text_data }}, which renders like this:

```
# Text content
Expand All @@ -29,8 +27,8 @@ Here is some text
```


You can also refer to the yaml metadata in your markdown files, using text_property_value:
You can also refer to the yaml metadata in your markdown files, using {{ _local_frontmatter.some_text_data.text_property }}:

```
text_property_value
```
29 changes: 29 additions & 0 deletions demo_book/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Variable variables for book demo

This folder contains an example of how to write a generic jinja template, and encode the contents within data variables in the config file.

There are three examples that produce the same output, which can be produced using these commands:

```
mm -c book_basic/_markmeld.yaml default -p
mm -c book_var1/_markmeld.yaml default -p
mm -c book_var2/_markmeld.yaml default -p
```


## Book basic

This encodes the chapters directly in the `jinja` template. It's simple, but it's not re-usable.

## Book var1

This method uses the `_md` array, and indexes into it with `data.variables` specified in the `_markmeld.yaml` file. This uses a jinja template that is reusable.

## Book var2 (recommended)

This way extracts the chapters out of the `_markmeld.yaml` config file into a standalone yaml file. This is the most flexible way, since both the `jinja` template can be reused, and the `target` could also be imported and re-used with a different `chapters.yaml` file.


10 changes: 10 additions & 0 deletions demo_book/book_basic/_markmeld.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
targets:
default:
jinja_template: book.jinja
output_file: "demo_output.txt"
command: cat > {output_file}
data:
md_files:
intro: ../md/01-intro.md
credit: ../md/02-credit.md
savings: ../md/03-savings.md
3 changes: 3 additions & 0 deletions demo_book/book_basic/book.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ intro }}
{{ credit }}
{{ savings }}
15 changes: 15 additions & 0 deletions demo_book/book_basic/demo_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Introduction

This is my introduction chapter

...
# Credit

This is chapter 2 on credit.

...
# Chapter 3: Savings

This is chapter 3 on savings.

...
15 changes: 15 additions & 0 deletions demo_book/book_var1/_markmeld.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
targets:
default:
jinja_template: book.jinja
output_file: "demo_output.txt"
command: cat > {output_file}
data:
md_files:
intro: ../md/01-intro.md
credit: ../md/02-credit.md
savings: ../md/03-savings.md
variables:
chapters:
- intro
- credit
- savings
2 changes: 2 additions & 0 deletions demo_book/book_var1/book.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% for chapter in chapters %}{{ _md[chapter] }}
{% endfor %}
15 changes: 15 additions & 0 deletions demo_book/book_var1/demo_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Introduction

This is my introduction chapter

...
# Credit

This is chapter 2 on credit.

...
# Chapter 3: Savings

This is chapter 3 on savings.

...
10 changes: 10 additions & 0 deletions demo_book/book_var2/_markmeld.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
targets:
default:
jinja_template: book.jinja
output_file: "demo_output.txt"
command: cat > {output_file}
data:
md_globs:
- ../md/*.md
yaml_files:
chapters: chapters.yaml
2 changes: 2 additions & 0 deletions demo_book/book_var2/book.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% for chapter in _yaml["chapters"] %}{{ _md[chapter] }}
{% endfor %}
3 changes: 3 additions & 0 deletions demo_book/book_var2/chapters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- 01-intro
- 02-credit
- 03-savings
15 changes: 15 additions & 0 deletions demo_book/book_var2/demo_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Introduction

This is my introduction chapter

...
# Credit

This is chapter 2 on credit.

...
# Chapter 3: Savings

This is chapter 3 on savings.

...
5 changes: 5 additions & 0 deletions demo_book/md/01-intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

This is my introduction chapter

...
5 changes: 5 additions & 0 deletions demo_book/md/02-credit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Credit

This is chapter 2 on credit.

...
5 changes: 5 additions & 0 deletions demo_book/md/03-savings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Chapter 3: Savings

This is chapter 3 on savings.

...
26 changes: 17 additions & 9 deletions demo_loop/_markmeld.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
version: 1
targets:
basetgt:
data:
md_files:
some_text_data: some_text.md
default:
inherit_from: basetgt
command: cat > "{output_file}"
output_file: "{_today}_demo_output_{recipient}.txt"
loop:
loop_data: recipients
assign_to: recipient
md_template: md_template.jinja
jinja_template: md_template.jinja
recursive_render: false
data_yaml:
- some_data.yaml
data:
yaml_globs_unkeyed:
- some_data.yaml
complex_loop:
inherit_from: basetgt
command: cat > "{output_file}"
output_file: "{today}_demo_output_complex_{recipient[name]}.txt"
jinja_template: md_template_complex.jinja
recursive_render: false
loop:
loop_data: recipients
assign_to: recipient
md_template: md_template_complex.jinja
recursive_render: false
data_yaml:
- complex_loop.yaml
data_md:
some_text_data: some_text.md
data:
yaml_globs_unkeyed:
- complex_loop.yaml

6 changes: 3 additions & 3 deletions demo_loop/md_template.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Which renders like this:

## Markdown data

You can refer to *content* in the markdown data with the `.content` attribute, using the name you put in the config file {% raw %}{{ some_text_data.content }}{% endraw %}, which renders like this:
You can refer to *content* in the markdown data with the `.content` attribute, using the name you put in the config file {% raw %}{{ some_text_data }}{% endraw %}, which renders like this:

```
{{ some_text_data.content }}
{{ some_text_data }}
```


You can also refer to the yaml metadata in your markdown files, using {% raw %}{{ some_text_data.metadata.text_property }}{% endraw %}:

```
{{ some_text_data.metadata.text_property }}
{{ _local_frontmatter.some_text_data }}
```
38 changes: 0 additions & 38 deletions docs/alternative_commands.md

This file was deleted.

Loading

0 comments on commit 72cede9

Please sign in to comment.