An open API service indexing awesome lists of open source software.

https://github.com/sebbekarlsson/gpp

General PreProcessor
https://github.com/sebbekarlsson/gpp

interpreter jinja jinja2 preprocessor static-site-generation static-site-generator templating templating-engine templating-language

Last synced: 8 months ago
JSON representation

General PreProcessor

Awesome Lists containing this project

README

          

# GPP
> General PreProcessor
> _not just for HTML, it can be used anywhere_

```html


{{ "Welcome" }}


The basics



Virtual file blocks and compute blocks



(@
Here is some text that will be completely ignored.
This: {{ "Is not ignored however" }}.
@)

Mapping a list to HTML



    {{ map (["apple", "banana", "pear","orange"], (@
  • {{ $0 }}
  • @)) }}

Dumping out file contents


{{ cat ("src/main.c") }}

Below will be evaluated


{{ cate ("examples/hello.html") }}



Integrating other languages



I really want this list to be rendered using Python!



    (@#!/usr/bin/python
    for name in ["john", "sarah", "anna"]:
    x = """(@

  • {}

  • @)""".format(name)
    print(x)
    @)

I do want this list to be rendered in Node.js though



    (@#!node
    console.log(["john", "sarah", "anna"]
    .map(name => (@ "
  • " + name + "
  • " @)).join('\n'));
    @)


```

## Templating
### The basics
> There are a few important tokens in the templating language,
> and we will go through them in this section.
#### {{ , }}
> The `{{` and the `}}` tokens are used to indicate the start `{{` and the end `}}`
> of a block of templating logic.
#### (@ , @)
> The `(@` and the `@)` tokens are used to indicate the start `(@` and the end `@)`
> of a virtual file block.
> A virtual file block is something that is just treated as text by the preprocessor.
> In other words, nothing within these tokens will be interpreted.
> The entire document is by default wrapped in a virtual file block.
> **There is one exception** to the `(@ @)` (virtual file block) however,
> if you specify a path to an interpreter, the rest of the content will be
> interpreted. _(There is an example further down in this document)_
#### If you still do not understand
> The following:
```html

{{ "hello" }}


```
> Will be rendered as:
```html

hello


```
> The following:
```html

(@ "hello" @)


```
> Will be rendered as:
```html

"hello"


```
#### Arrow functions
> You can also define functions to render data,
> Here is an example of creating a function to render a `
    ` list:
    ```jsx
    {{
    itemList = (items) => map (items, (@
  • {{ $0 }}

  • @))
    }}

    {{ itemList (["hello", "world"]) }}
    ```
    > This will render as:
    ```html


    • hello

    • world


    ```
    ### Extending / Inheritance
    > You can make templates inherit / extend other templates.
    > Here is how a parent template could look like: (`parent.hml`)
    ```html


    (@#%block title @)


    (@#%block body @)

    ```
    > Notise the `%` at the start of the comment.
    > And then you can write a child template that inherits from this one
    > Like this: (`child.html`)
    ```html
    #%extends "parent.html"

    (@#%block title
    My Title
    @)

    (@#%block body
    Welcome to my website
    @)
    ```
    > Also, notice here the `%` at the start of each comment.
    > If you run `gpp` over `child.html` now, the output will be:
    ```html


    My Title

    Welcome to my website

    ```

    ## Loading data
    > What is a templating engine without any data to work with?
    > To access data in your templates, simply put a `context.json` file
    > within the root of your project.
    > Example (`context.json`):
    ```json
    {
    "title": "My Title",
    "favouriteFruits": ["apple", "pear", "banana"]
    }
    ```
    > Now you can access these variables in your templates,
    > Example:
    ```html

    {{ title }}

    {{ map (favouriteFruits, (@

  • {{ $0 }}
  • @)) }}

    ```
    > You can also use the `load` function. Like this:
    ```html
    {{
    mydata = load("somedata.json")
    }}

    {{ mydata.firstname }}


    ```

    ## Any interpreter you want
    > If you do not want to use the default language,
    > you can use any language you want to interpret the templates.
    > Here is an example where we are rendering a `

      ` list using `Python`.
      ```html



        (@#!/usr/bin/python
        for i in range(100):
        x = """(@

      • {}

      • @)""".format(i)
        print(x)
        @)

      ```
      > To make this work, we simply put a comment inside our `(@ @)` block.
      > This comment should start with a `#!` and then the path to the
      > program that should be interpreting the rest of the content inside of the
      > `(@ @)` block.

      ## Installation
      > Ready to use this piece of software?
      ### 1. Clone it (or download it)
      > Go and get the source code, if you are reading this on Github,
      > then just copy the clone URL and clone it down.
      ```
      git clone --recurse-submodules
      ```
      > Do NOT forget the `--recurse-submodules` flag.
      ### 2. Compile it
      > Make sure you have a compiler on your system, preferably `gcc`.
      > You can read more about `gcc` here: [https://gcc.gnu.org/](https://gcc.gnu.org/)
      > You also need to make sure you have `make` on your system.
      > You can read more about `make` here: [https://www.gnu.org/software/make/](https://www.gnu.org/software/make/).
      > **Alright!** time to compile this thing.
      >
      > **Go to the root directory** - Open up a shell and go to the root directory of this project:
      ```bash
      cd gpp/
      ```
      > **Execute Make** - Run the make command
      ```bash
      make
      ```
      > **!!!Abrakadabra!!!**
      ### 3. Run it
      > You are now ready to use this,
      > Here is how you do that:
      ```bash
      ./a.out index.html
      ```
      > Yes... I know `a.out` is not really a production ready name for a binary.
      > But this software is still in the _work in progress_ stage.
      > Anyways, so you execute the `a.out` file and you give it a filename as
      > the first argument and then it will just print out the transpiled / compiled
      > content to `stdout`.

      ## Plans
      > Here are some plans / thoughts.
      > If you feel like you could help implement some of them, feel free
      > to make a pull-request.
      - [ ] Write better documentation
      - [ ] Implement a step-by-step debugger
      - [ ] Create a shared library to allow the use of this in other languages
      - [ ] Implement bindings for NodeJS
      - [ ] Implement bindings for Python
      > Now, these are not tasks / issues.
      > This is just some ideas and I am just thinking out loud.
      > Bugs, Todos, and tasks will be seen in Github's issues section.

      ## FAQ
      ```
      If I define a variable in my Python code,
      how can I later access it in my NodeJS block
      below?
      ```
      > That is unfortunately not possible.
      > If you want to share data between languages, you need to use the built-in
      > templating language to unserialize the data into your code blocks.
      ```
      Isn't it unsafe to call the binary specified in the "!#" comment?
      ```
      > Well, just don't put an unsafe binary there?
      > This couldn't care less about what logic you write or which
      > binaries you use to interpret the code blocks.
      > This is just a templating language.
      ```
      Does it work on Windows?
      ```
      > I dont know, I haven't tried it there.
      > But I assume it would be tricky to get it to work there,
      > since this software was developed on a Linux machine and the source code
      > has dependencies on C implementations on Linux... you know header files
      > and stuff.
      ```
      How do I send data into the template context?
      ```
      > Put a `context.json` file in the root of your project,
      > you can later access all data in this file inside your templates.

      ## Status
      ![Compile and test](https://github.com/sebbekarlsson/gpp/workflows/Compile%20and%20test/badge.svg)

      ---
      **

      :coffee: Not Made with Coffee and Love in San Francisco :coffee:
      **