foundation / panini

A super simple flat file generator.
Other
592 stars 104 forks source link

Generate pages on the fly from data #159

Open dagostinelli opened 6 years ago

dagostinelli commented 6 years ago

Current State Put HTML and markdown into .html files stored in /pages and you get a url per page:

/pages
..../home.html
..../contactus.html
..../info1.html
/layouts
..../default.html
/data
....<empty>

$ foundation build

// resulting URLs
http://example.com/home.html
http://example.com/contactus.html
http://example.com/info1.html

Requested Capability Put HTML and markdown into a hierarchical JSON doc get a url per page:

/pages
..../empty
/layouts
..../default.html
/data
....pages.json (this is a 10MB JSON file with a record per page)

$ foundation build

// resulting URLs
http://example.com/home.html
http://example.com/contactus.html
http://example.com/info1.html

I have a use-case for this. My project has a SQL database and we'd like to create a static, read-only site where there is 1 page per record. I can write a program to create the json file from the database and I can also generate a whole bunch of /pages/.html files from the database. In current state, I will have to generate a whole bunch of /pages/.html files.

Potential API: The root parameter can take a directory for physical .html files and/or a hierarchical JSON doc.

var jsonDocOfPagesData = <generate a hierarchical json doc, records for pages and directories>

/// ...

.pipe(panini({
  root: ['src/pages/', jsonDocOfPagesData],
  layouts: 'src/layouts/',
  partials: 'src/partials/',
  data: 'src/data/',
  helpers: 'src/helpers/'
}))
gakimball commented 6 years ago

I have ideas for the API for this, but they aren't finalized.

What I currently have in the v2 alpha of Panini is the ability to convert any kind of file into a page. You write a function that takes in files matching a pattern like /blog-posts/*.md, and each file can be converted into a page.

What I don't have is the ability to turn any arbitrary data source into a page. In your example, you're converting a single file, a JSON file, into multiple pages.

dagostinelli commented 6 years ago

In another variation, perhaps the output can be like this where each file under plant_records is a single record turned into a file.

/dist
..../home.html
..../contactus.html
..../plant_records
..../plant_records/24.html
..../plant_records/25.html
..../plant_records/26.html
..../plant_records/27.html
..../plant_records/28.html
..../plant_records/29.html
w-biggs commented 5 years ago

I'm interested in this as well. If I have a JSON array, and I want to generate a page for each item in the array, where that item is passed into that page as data, there doesn't seem to be a way to do that currently.

lendlsmith commented 5 years ago

@gakimball can you post a copy of this function please. Currently i am only getting it to work if i put the file name but not if i use /*.md

TD540 commented 5 years ago

@w-biggs @dagostinelli Anyone found a good solution to this? I'm looking for the same thing :)

erutan commented 4 years ago

Also looking for /*.md functionality.

dagostinelli commented 4 years ago

@TD540 @w-biggs @erutan I ended up writing my own. I called it spoonbill

https://github.com/dagostinelli/spoonbill

It doesn't use Foundation per-se. You can actually use anything you want with it. Foundation included.

Basically, I replaced Panini with a Makefile and a python script that can take any-old .md file and spit out HTML. Ask me about it over at the spoon bill repository if you want to see examples of how to use it.

Though simple, Spoonbill and it's Makefile together are actually very powerful.

dagostinelli commented 4 years ago

Oh, and to do the data.json -> .md conversion, I used another python script to do that. It is custom made for the web sites (can't really share it) But you can imagine how it might work.

data.json -> convert.py -> tons of .md files

/src
..../home.md
..../contactus.md
..../plant_records
..../plant_records/24.md
..../plant_records/25.md
..../plant_records/26.md
..../plant_records/27.md
..../plant_records/28.md
..../plant_records/29.md

Then I used spoonbill to convert the .md files into .html files (via the makefile)

/dist
..../home.html
..../contactus.html
..../plant_records
..../plant_records/24.html
..../plant_records/25.html
..../plant_records/26.html
..../plant_records/27.html
..../plant_records/28.html
..../plant_records/29.html

Works fast and does a great job.

erutan commented 4 years ago

There’s plenty proper static site generators out there - I just use this occasionally for the speed / simplicity. :)

Someone else was inputting content, and markdown preview didn’t want to show up on their editor due to the file extension. I ended up just telling it to treat all .html extensions as markdown.