Skip to content

conao3/org-generate.el

Repository files navigation

org-generate.el

https://raw.githubusercontent.com/conao3/files/master/blob/headers/png/org-generate.el.png https://img.shields.io/github/license/conao3/org-generate.el.svg?style=flat-square https://img.shields.io/github/tag/conao3/org-generate.el.svg?style=flat-square https://github.com/conao3/org-generate.el/workflows/Main%20workflow/badge.svg

Generate template files and directories from a single org document.

Overview

org-generate.el provides a simple and powerful way to define and generate file/folder templates using Org mode. Define your templates once in an org file, then generate them anywhere with a single command.

Key features:

  • Define templates using familiar Org mode syntax
  • Support for single files, multiple files, or entire project structures
  • Variable substitution using Mustache templates
  • Interactive template selection with completion

Requirements

  • Emacs 26.1 or later
  • Org mode 9.3 or later
  • mustache.el 0.23 or later

Installation

(use-package org-generate
  :ensure t)

Or with leaf:

(leaf org-generate :ensure t)

Quick Start

  1. Create a template file at ~/.emacs.d/org-generate.org (or customize org-generate-file)
  2. Define your templates using org headings
  3. Run M-x org-generate and select a template

Template Syntax

Templates are organized in a two-level hierarchy:

  • First level: Category (e.g., hugo, project)
  • Second level: Template name (e.g., page, single)
  • Third level and beyond: File and directory definitions

Single File Template

* hugo
** page
:PROPERTIES:
:org-generate-root: ~/dev/repos/hugo-src/content/blog/
:END:

*** page.md
#+begin_src markdown
---
title: "New Post"
date: 2024-01-01
draft: true
---

Content goes here.
#+end_src

Run M-x org-generate and select hugo/page to create the file.

Using Variables

Use Mustache syntax for dynamic content. Declare variables in the org-generate-variable property:

* hugo
** single
:PROPERTIES:
:org-generate-root: ~/dev/repos/hugo-src/content/blog/
:org-generate-variable: title date
:END:

*** {{title}}.md
#+begin_src markdown
---
title: {{title}}
date: {{date}}
draft: true
---

Content goes here.
#+end_src

When you generate this template, you’ll be prompted for title and date values.

Multi-File Project Template

Create entire project structures with a single template:

* project
** elisp
:PROPERTIES:
:org-generate-root: ~/dev/repos/
:org-generate-variable: pkg-name description
:END:

*** {{pkg-name}}.el/
**** .github/
***** workflows/
****** test.yml
#+begin_src yaml
name: Main workflow
on: [push, pull_request]
#+end_src

**** .gitignore
#+begin_src gitignore
*-autoloads.el
*.elc
/.keg
#+end_src

**** README.org
#+begin_src org
,,* Description
{{description}}
#+end_src

**** {{pkg-name}}.el
#+begin_src emacs-lisp
;;; {{pkg-name}}.el --- {{description}}  -*- lexical-binding: t; -*-

;;; Code:

(provide '{{pkg-name}})
;;; {{pkg-name}}.el ends here
#+end_src

Note: Directory names must end with / to be recognized as directories.

Commands

  • org-generate - Generate files from a selected template
  • org-generate-edit - Open the template file for editing

Configuration

VariableDescriptionDefault
org-generate-filePath to the template definition file~/.emacs.d/org-generate.org
org-generate-with-export-as-orgExport as org before generatingt

When org-generate-with-export-as-org is enabled, you can use additional Org features like file includes, macros, and noweb references in your templates.

Contributing

Contributions are welcome. Please feel free to submit issues and pull requests.

Development Setup

Install keg for testing:

cd ~/
git clone https://github.com/conao3/keg .keg
export PATH="$HOME/.keg/bin:$PATH"

Running Tests

make test

License

GPL-3.0. See LICENSE for details.

Author

Naoya Yamashita (conao3)

About

Generate template files/folders from one org document

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •