Skip to content

Instantly share code, notes, and snippets.

@noteed
Last active January 9, 2023 21:53

Revisions

  1. noteed revised this gist Aug 22, 2021. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions html.jq
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    def td: "<td>\(.)</td>";
    def th: "<th>\(.)</th>";
    def tr:"<tr>\(.)</tr>";
    def table: "<table>\(.)</table>";
    def tbody: "<tbody>\(.)</tbody>";
    def thead: "<thead>\(.)</thead>";

    def render_as_table:
    "\(
    to_entries | map(.key|th) | join("") | tr|thead
    )\n\(
    to_entries | map("\(
    .value |
    if type=="object" then
    render_as_table|td
    elif type=="array" then
    render_as_table|td
    else
    td end
    )") | join("") | tr | tbody
    )" | table ;
  2. noteed revised this gist Sep 13, 2020. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Pandoc, YAML, JSON, CSV, Sqlite
    # Pandoc, YAML, JSON, CSV, SQLite

    This gists shows a little idea: use Pandoc to format data into HTML, or any
    other output format supported by Pandoc. This uses the ability of Pandoc to
    @@ -9,6 +9,11 @@ It also shows a few scripts to use other data formats than YAML and extract
    metadata blocks as JSON. Note that multiple metadata blocks can be present
    within a single document.

    See also [this
    Gist](https://gist.github.com/noteed/012e9e8d9744f7204fa2abfee629ce00) to see a
    small Scotty-based web server using full-text search against a SQLite database
    containing Markdown files.


    ## Self-contained Pandoc document

    @@ -67,11 +72,11 @@ used because Pandoc is left-biased when merging multiple metadata blocks.
    Several scripts are provided to use other data format. For instance:

    - From `hosts.json` (one object per line) to CSV,
    - From CSV to a Sqlite database,
    - From a Sqlite database to JSON again (a list of objects),
    - From a Sqlite database to YAML (using the previous script).
    - From CSV to a SQLite database,
    - From a SQLite database to JSON again (a list of objects),
    - From a SQLite database to YAML (using the previous script).

    This shows that the data rendered by Pandoc can come from Sqlite with minimal
    This shows that the data rendered by Pandoc can come from SQLite with minimal
    efforts.


    @@ -84,3 +89,13 @@ using a dedicated template (here, `metadata.tpl`):
    $ pandoc --template metadata.tpl document.md
    {"title":"The document title","host":[{"ip":"172.17.0.1","name":"host-1"},{"ip":"172.17.0.2","name":"host-2"}]}
    ```


    ## TODO

    Rendering JSON as a nested table (with the appropriate definitions in
    `html.jq`):

    ```
    $ echo '{"a": {"b": {"c": 2}}, "d": 3}' | jq -r 'include "html"; render_as_table'
    ```
  3. noteed revised this gist Apr 7, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    hosts.csv
    hosts.db
  4. noteed revised this gist Nov 6, 2019. 2 changed files with 26 additions and 4 deletions.
    29 changes: 25 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    # Pandoc, YAML, JSON, CSV, Sqlite

    Use Pandoc to format data.
    This gists shows a little idea: use Pandoc to format data into HTML, or any
    other output format supported by Pandoc. This uses the ability of Pandoc to
    uses templates, and to receive data as YAML either from a separate file, or
    within a metadata block contained in the document itself..

    It also shows a few scripts to use other data formats than YAML and extract
    metadata blocks as JSON. Note that multiple metadata blocks can be present
    within a single document.


    ## Self-contained Pandoc document
    @@ -26,12 +33,12 @@ Some hosts:
    ```

    The result is a normal Markdown document that can be processed with Pandoc as
    usual.
    usual, e.g. to produce an HTML page.


    ## Separate data

    The data can also come from an external file:
    The data can also come from an external file (here, `hosts.yml`):

    ```
    $ pandoc --template document.md hosts.yml document.md
    @@ -57,9 +64,23 @@ used because Pandoc is left-biased when merging multiple metadata blocks.

    ## Other formats

    Several scripts are provided to change data format. For instance:
    Several scripts are provided to use other data format. For instance:

    - From `hosts.json` (one object per line) to CSV,
    - From CSV to a Sqlite database,
    - From a Sqlite database to JSON again (a list of objects),
    - From a Sqlite database to YAML (using the previous script).

    This shows that the data rendered by Pandoc can come from Sqlite with minimal
    efforts.


    ## Extract metadata block

    It is also possible to extract the data "stored" within a metadata block by
    using a dedicated template (here, `metadata.tpl`):

    ```
    $ pandoc --template metadata.tpl document.md
    {"title":"The document title","host":[{"ip":"172.17.0.1","name":"host-1"},{"ip":"172.17.0.2","name":"host-2"}]}
    ```
    1 change: 1 addition & 0 deletions metadata.tpl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    $meta-json$
  5. noteed revised this gist Nov 6, 2019. No changes.
  6. Your Name revised this gist Nov 6, 2019. 8 changed files with 109 additions and 10 deletions.
    66 changes: 65 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,65 @@
    #
    # Pandoc, YAML, JSON, CSV, Sqlite

    Use Pandoc to format data.


    ## Self-contained Pandoc document

    The `document.md` file contains data in its metadata YAML block and is its own
    template:

    ```
    $ pandoc --template document.md document.md
    ---
    title: The document title
    host:
    - name: host-1
    ip: 172.17.0.1
    - name: host-2
    ip: 172.17.0.2
    ---
    Some hosts:
    - host-1: 172.17.0.1
    - host-2: 172.17.0.2
    ```

    The result is a normal Markdown document that can be processed with Pandoc as
    usual.


    ## Separate data

    The data can also come from an external file:

    ```
    $ pandoc --template document.md hosts.yml document.md
    ---
    title: The document title
    host:
    - name: host-1
    ip: 172.17.0.1
    - name: host-2
    ip: 172.17.0.2
    ---
    Some hosts:
    - host-1: 172.17.0.1
    - host-2: 172.17.0.2
    - host-3: 172.17.0.3
    ```

    Note: here the `hosts` key in the metadata provided by `document.md` is not
    used because Pandoc is left-biased when merging multiple metadata blocks.


    ## Other formats

    Several scripts are provided to change data format. For instance:

    - From `hosts.json` (one object per line) to CSV,
    - From CSV to a Sqlite database,
    - From a Sqlite database to JSON again (a list of objects),
    - From a Sqlite database to YAML (using the previous script).
    7 changes: 7 additions & 0 deletions csv-to-sqlite.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #! /usr/bin/env bash

    sqlite3 hosts.db -cmd '.mode csv' '.import hosts.csv hosts'

    # Demonstrate the import succeeded
    sqlite3 hosts.db '.schema hosts'
    sqlite3 hosts.db 'select * from hosts'
    3 changes: 3 additions & 0 deletions hosts.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    { "name":"host-1", "ip":"172.17.0.1" }
    { "name":"host-2", "ip":"172.17.0.2" }
    { "name":"host-3", "ip":"172.17.0.3" }
    9 changes: 0 additions & 9 deletions hosts.yml
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    ---
    host:
    - name: host-1
    ip: 172.17.0.1
    - name: host-2
    ip: 172.17.0.2
    - name: host-3
    ip: 172.17.0.3
    ---
    7 changes: 7 additions & 0 deletions json-to-csv.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #! /usr/bin/env bash

    # Construct the CSV header
    head -1 hosts.json | jq -r 'keys | @csv' > hosts.csv

    # Extract rows
    cat hosts.json | jq -r '[.[]] | @csv' >> hosts.csv
    5 changes: 5 additions & 0 deletions sqlite-to-json.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #! /usr/bin/env bash

    sqlite3 hosts.db \
    "select json_group_array(json_object('ip', ip, 'name', name)) as host
    from (select * from hosts)" | jq .
    6 changes: 6 additions & 0 deletions sqlite-to-yaml.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #! /usr/bin/env bash

    echo '---'
    echo host:
    ./sqlite-to-json.sh | jq -r -f to-yaml.jq
    echo '---'
    16 changes: 16 additions & 0 deletions to-yaml.jq
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    def y:
    (objects | to_entries[] | (.value | type) as $type |
    if $type == "array" then
    "\(.key):", (.value | y)
    elif $type == "object" then
    "\(.key):", " \(.value | y)"
    else
    "\(.key): \(.value)"
    end
    )
    // (arrays | select(length > 0)[] | [y] |
    " - \(.[0])", " \(.[1:][])"
    )
    // .
    ;
    y
  7. Your Name revised this gist Nov 6, 2019. 2 changed files with 23 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions document.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    ---
    title: The document title
    host:
    - name: host-1
    ip: 172.17.0.1
    - name: host-2
    ip: 172.17.0.2
    ---

    Some hosts:

    $for(host)$
    - $host.name$: $host.ip$
    $endfor$
    9 changes: 9 additions & 0 deletions hosts.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    ---
    host:
    - name: host-1
    ip: 172.17.0.1
    - name: host-2
    ip: 172.17.0.2
    - name: host-3
    ip: 172.17.0.3
    ---
  8. noteed created this gist Nov 6, 2019.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #