sc0ttj / mdsh

A simple static site generator, using Markdown and Bash
https://sc0ttj.github.io/mdsh/
10 stars 0 forks source link

Copy the Jekyll "Data Files" feature #65

Closed sc0ttj closed 5 years ago

sc0ttj commented 5 years ago

From https://jekyllrb.com/docs/datafiles/:

Data Files

In addition to the built-in variables available from Jekyll, you can specify your own custom data that can be accessed via the Liquid templating system.

Jekyll supports loading data from YAML, JSON, CSV, and TSV files located in the _data directory. Note that CSV and TSV files must contain a header row.

This powerful feature allows you to avoid repetition in your templates and to set site specific options without changing _config.yml.

The Data Folder The _data folder is where you can store additional data for Jekyll to use when generating your site. These files must be YAML, JSON, or CSV files (using either the .yml, .yaml, .json or .csv extension), and they will be accessible via site.data.

To do

  1. create a assets/data folder
  2. add a data file in there with the same name as an existing post
  3. write some functions to process the data file into shell variables/hashes/arrays
  4. make those variable available to the build scripts and templates at build time

Ideas

Structure:

assets/data
assets/data/foo.json
assets/data/bar.yaml
assets/data/my-cool-post/
assets/data/my-cool-post/something.json
assets/data/my-cool-post/more-data.yaml

^ Both foo.json and bar.yaml would be made available to all pages, in the site_data.. so their contents are available to the Markdown (and sub-shells), the templates and build scripts/process generally (as shell vars, arrays, etc)

But, the files in data/my-cool-post should be parsed only when building from the file my-cool-post.mdsh.

JSON and YAML parsing

more-data.yaml:

  page_details
    price: 100.00
    features: [ 'cheap', 'good' , 'awesome' ]

something.json:

  { 
    "page_details" :  {
        "price": "100.00",
        "features": [ "cheap", "good" , "awesome" ]
    }
  }

In both cases, the data would be made available to during the build process as:

${page_details[@]}              # equals 'price features'
${page_details[price]}            # equals '100.00'
${page_details[features]}           # equals 'cheap good awesome'

${page_details_features[@]}     # equals 'cheap good awesome'
${page_details_features[1]}     # equals 'good'
${page_details_features[2]}     # equals 'awesome'

$page_details_price             # equals '100.00'
$page_details_features_0        # equals 'cheap'
$page_details_features_1        # equals 'good'
$page_details_features_2        # equals 'awesome'

^ Once the JSON/YAML parsers decares the above, these variables and arrays/hashes should be available in the templates like any others ($page_title, etc).

Using parsed data in templates

Once parsed, the data could be used in templates like so:

Indexed arrays:

{{#page_details_features}}
  * {{.}}
{{/foreach}}

Associative arrays:

Easier associative arrays

It will be easier to use associative arrays in templates after the foreach function is implemented as an iterator in mo:

Associative arrays, using foreach:

{{#foreach detail in page_details}}
  Price: {{detail.price}}
{{/foreach}}

Indexed arrays, using foreach:

{{#foreach feature in page_details_features}}
  * {{feature}}
{{/foreach}}

Useful libraries

As shell by default is piss poor with multi-dimensional data stuctures (like JSON, CSV and YAML), we need to add some Shell, AWK, Python or Perl scripts to the functions/ dir, which can then be used to make the yaml/csv/json data available to our build process and templates :)

Essentially, the ideal scripts:

Shell scripts:

AWK scripts:

Perl scripts:

Python scripts:

sc0ttj commented 5 years ago

Possible liquid filters

After this issue is resolved, these liquid filters should be easy enough:

sc0ttj commented 5 years ago

PR here: https://github.com/sc0ttj/mdsh/pull/85

sc0ttj commented 5 years ago

done - merged https://github.com/sc0ttj/mdsh/pull/85