Skip to content

Instantly share code, notes, and snippets.

@mistic100
Last active May 1, 2021 09:17
Show Gist options
  • Select an option

  • Save mistic100/794319529c410e15ecd6325131602d47 to your computer and use it in GitHub Desktop.

Select an option

Save mistic100/794319529c410e15ecd6325131602d47 to your computer and use it in GitHub Desktop.

Revisions

  1. mistic100 renamed this gist May 1, 2021. 1 changed file with 0 additions and 0 deletions.
  2. mistic100 renamed this gist May 1, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mistic100 revised this gist May 10, 2018. No changes.
  4. mistic100 created this gist May 10, 2018.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Gallery generator for [Phugo](https://github.com/aerohub/phugo)

    For each directory in `content` it generates or updates the `_index.md` file with the list of all JPG files in the directory.

    If `_index.md` already exists, it's metadata will be kept, the rest is overwritten.

    If the metadata of the `_index.md` file contains `manual: true`, the directory is ignored.

    The thumbnails must be named by the original name prefixed by `_th_`.
    40 changes: 40 additions & 0 deletions generate.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const fs = require('fs');

    fs.readdirSync('content')
    .filter(entry => fs.statSync(`content/${entry}`).isDirectory())
    .forEach(dir => {
    let files = fs.readdirSync(`content/${dir}`)
    .filter(file => file.endsWith('.jpg'))
    .filter(file => !file.startsWith('_th_'));

    let content;

    if (fs.existsSync(`content/${dir}/_index.md`)) {
    const lines = fs.readFileSync(`content/${dir}/_index.md`, {encoding : 'utf8'}).split('\n');

    if (lines.indexOf('manual: true') !== -1) {
    return;
    }

    const headerEnd = lines.lastIndexOf('---');

    content = lines.slice(0, headerEnd + 1).join('\n');

    } else {
    content = `---
    title: "${dir}"
    albumthumb: "${dir}/_th_${files[0]}"
    date: ${new Date().toISOString()}
    ---`;
    }

    content+= '\n\n';

    content+= files
    .map(file => `{{< photo full="${file}" thumb="_th_${file}" >}}`)
    .join('\n');

    content+= '\n';

    fs.writeFileSync(`content/${dir}/_index.md`, content, {encoding : 'utf8'});
    });