giftsnippet 0.2.4
Integrate highlighted source code images with data uris into your gift questions
License: BSD
Keywords: gift moodle code-formatting

# Gift Snippet

The [GIFT format](https://docs.moodle.org/311/en/GIFT_format) allows for managing student questions for learning management systems within text files. Integrating images requires a rather cumbersome approach by using data URIs. Additionally, if you want to integrate source code one has to transform the source into an image and than integrate the image via a data URI.

> Gift Snippet aims to simplify integrating images and source code images into gift questions

The following gift question demonstrates the problem:

```
[html] <p>What is the correct output of the following code snippet?\:</p><br>
<img src\="data\:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuAAAABlCAYAAAAMPFZLAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAxmSURBVHhe7d1RaFvXHcfxnwrbWCmEzeA5S1Oikoe+1PVaPSRy6YMxuHQQj5i2m+1SUMNWKNhmFPoQ20KW81AYwxYM2pIIutpu0xIzB1ZqMIaVxg7UZa67R683tMliDF5nCH3IRrR77j2yJUWy5bS5uo6+H7i555x75bz+OPzv+UdyLgEAAAAIxH32DgAAACAABHAAAAAgQARwAAAAIEAEcAAAACBAVX2EmUql7AgAAABAJclk0o4quysBvJr/GAAAALiXmMxcTQ6mBAUAAAAIEAEcAAAACBABHAAAAAgQARwAAAAIEAEcAAAACBABHAAAAAhQyAL4mqbeiCjyxpQ7qpb9zev+1TVX/S8X35pT5Hf2esuxq1VaHlOsb0YbdmqsZGIaW7YTAAAAoIx9vwO+eP6gejZPavLFnHKv5XShvck+2dnaXz9W/DNp9OV25d50r99G7RPD0RkvmH+qRbtSZH1G/accDQ12qsEuGc19WelUv2bW7QIAAABQYp8H8DU537i3A11qqy53b3Gu3XT/PaC2X/jzLX//1A3e/9Sgnd5uQzOjaUXPjquz0S5tadbA2ajSo8U74wAAAEBeKAL44vl8CYnZzbaL1vYzc3VpqvoKkzvwb039ZdPbFV94wi6VWn5HaQ3phRY7L9Xygvs0rXduK0Vxg3tf7LayFQAAANSXmgdwE7DjV/IlJN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kAAAAEJyQB3HyMWXD2d17LgD0XvOAqOhWlQZ2JXk1kC0N5oRW9MyIN/brajzoBAACAu2v/14CbhjtH0+oo0/VyJZOQMzx8e7AHAAAAauSe+AizuW9WQ6vnihvumAY9ymr8BMUnAAAACI97IoB7pSiZkhIWU75S9XniAAAAQDDukQAOAAAA7A8EcAAAACBABHAAAAAgQARwAAAAIECRnMuOK0qlUnYEAAAAoJJkMmlHlVUVwAEAAAB8PyhBAQAAAAJEAAcAAAACRAAHAAAAAkQABwAAAAJEAAcAAAACI/0fVekIJSLh9EEAAAAASUVORK5CYII\="> 
{
    =15
    ~Error
    ~12
    ~1512
}
```

Rendered in Moodle the question would look like this:

![Moodle Question](./docs/moodle.png "Moodle Question")

To generate the data uri images from real source code files or existing images and keep it in sync with a gift file `giftsnippet` can be used.

## Installation

```bash
pip install giftsnippet
```

## Usage

Let's assume you have the following folder structure:

```text
questions.txt
code_samples/
    example_01.py
    example_02.py
images/
    sample_image.png
    ...
```

In your `questions.txt` file you would like to integrate a formatted version of the Python code from e.g. `code_samples/example_01.py`. To do that you could use the `data-gift-snippet` HTML-Attribute and reference the respective code file within an `img` tag:

```text
[html] <p>What is the correct output of the following code snippet?\:</p><br>
<img data-gift-snippet\="code_samples/example_01.py">
{
    =15
    ~Error
    ~12
    ~1512
}
```

Afterward you could execute the `giftsnippet` command line tool to add the respective data uri to your questions. The `giftsnippet` command should be executed inside the folder listing the questions, with a reference to the respective gift file (`questions.txt` in the example):

```text
giftsnippet questions.txt
```

The result would look like this:

```text
[html] <p>What is the correct output of the following code snippet?\:</p><br>
<img data-gift-snippet\="code_samples/example_01.py" src\="data\:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuAAAABlCAYAAAAMPFZLAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAxmSURBVHhe7d1RaFvXHcfxnwrbWCmEzeA5S1Oikoe+1PVaPSRy6YMxuHQQj5i2m+1S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+AQAAEB4h+AizQ+mjWQ20uNPGTg0PO0rE+jWz7r9R0dU1dZkjBg9/q2l3Ot297h05eOayOzGnoJi6b91Sz+Ht4wi9ZwAAAEANRXIuO64olUrZUXWSyaQdAQAAAPXBZOZqcnCIasABAACAex8BHAAAAAgQARwAAAAIEAEcAAAACBABHAAAAAgQARwAAAAIUHgC+PqM+rc6XlZxDngRc554TGPLdmosjymWybf0AQAAAMIhJAHcDdCjaUXP5jtejquz0T4yQdoL5dsdMkutZAqa+eS1DCirhPovVte4HgAAAAhCSAL4NTkLrYr+3E6tjYv9imWjmj3ba1fKcAN6YnVIs33NdmFbc19W0ZGRPe6mAwAAAHdPjQP4isa83e2EJnRJ6WdsCYotHWk4Ma6lTKcavFk5G5rJTqg3UemdZr0wLKXfoxQFAAAA4VDjAN6sAa/kJKtetWroQ1uCUmY3uzyzc96rtsLSkxINx9rU+uf5iuUrAAAAQJDC8xHmnVh35MSjOmSnZTVGFbVDAAAAoNb2dwAHAAAA9pn9HcDN7vaCo2t2WlY1u+QAAABAQMIdwPNHEJ6acCcTSphx34y2DxY8pGh8QvOF53+X2Lg8r0tHozt8yAkAAAAEJyQB3HyMWXD2d17LgD0XvOAqOhWlQZ2JXk1kC0N5oRW9MyIN/brajzoBAACAu2v/14CbhjtH0+oo0/VyJZOQMzx8e7AHAAAAauSe+AizuW9WQ6vnihvumAY9ymr8BMUnAAAACI97IoB7pSiZkhIWU75S9XniAAAAQDDukQAOAAAA7A8EcAAAACBABHAAAAAgQARwAAAAIECRnMuOK0qlUnYEAAAAoJJkMmlHlVUVwAEAAAB8PyhBAQAAAAJEAAcAAAACRAAHAAAAAkQABwAAAAJEAAcAAAACI/0fVekIJSLh9EEAAAAASUVORK5CYII\="> 
{
    =15
    ~Error
    ~12
    ~1512
}
```

After any change in the source code files the command could be executed again to sync the gift questions with the source code.

## Supported snippets

### Code highlighting

```text
<img data-gift-snippet\="code_samples/example_01.py">
```

### Images 

```text
<img data-gift-snippet\="images/sample_image.jpg">
```

## Testing

```bash
python setup.py test
```

## Deployment

```text
python setup.py build sdist bdist_wheel
python -m twine upload dist/*
```