Skip to content

Commit

Permalink
new forms plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed May 23, 2015
1 parent 2a5db98 commit 49ebb68
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
57 changes: 57 additions & 0 deletions v7/forms/README.md
@@ -0,0 +1,57 @@
This plugin uses [Alpaca Forms](http://alpacajs.org/) to allow "easy" form creation
in reStructuredText documents.

Here's an example:

```
.. form::
{
"schema": {
"title": "What do you think of Alpaca?",
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"ranking": {
"type": "string",
"title": "Ranking",
"enum": ['excellent', 'not too shabby', 'alpaca built my hotrod']
}
}
}
```

Instead of using the form description as content for the directive, you can put it in a separate file
and load it like this:

```
.. form::
:file: formdescription.json
```

A description of how Alpaca works is beyond the scope of this README, and you should read
[their fine docs](http://alpacajs.org/tutorial.html) instead.

You will probably want to add something like this to your config:


```
EXTRA_HEAD_DATA += """
<link type="text/css" href="//code.cloudcms.com/alpaca/1.5.8/bootstrap/alpaca.min.css" rel="stylesheet" />
"""
BODY_END += """
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.js"></script>
<script type="text/javascript" src="//code.cloudcms.com/alpaca/1.5.8/bootstrap/alpaca.min.js"></script>
<script>
$(document).ready(function() {
$('div.alpacaform').each(function(index, element) {
data = eval(element.id+'_data');
Alpaca(element, data);
});});
</script>
"""
```
10 changes: 10 additions & 0 deletions v7/forms/forms.plugin
@@ -0,0 +1,10 @@
[Core]
Name = rest_forms
Module = forms

[Documentation]
Author = Roberto Alsina
Version = 0.1
Website = http://getnikola.com
Description = Alpaca-based forms for ReST.

49 changes: 49 additions & 0 deletions v7/forms/forms.py
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# This file is public domain according to its author, Brian Hsu

import uuid

from docutils.parsers.rst import Directive, directives
from docutils import nodes

from nikola.plugin_categories import RestExtension


class Plugin(RestExtension):

name = "rest_form"

def set_site(self, site):
self.site = site
directives.register_directive('form', AlpacaForms)
return super(Plugin, self).set_site(site)


class AlpacaForms(Directive):
""" Embed AlpacaForm form
Usage:
.. form::
[Alpaca form description as JSON]
.. form::
:file: formdescription.json
"""

option_spec = {'file': directives.unchanged}
has_content = True

def run(self):
formname = 'form_'+uuid.uuid4().hex
fname = self.options.get('file')
if fname is None:
data = '\n'.join(self.content)
else:
with open(fname, 'rb') as fd:
data = fd.read()
return [nodes.raw('', '''
<div id={formname} class="alpacaform"></div> <script type="text/javascript"> {formname}_data={data};</script>'''.format(formname=formname, data=data), format='html')]

0 comments on commit 49ebb68

Please sign in to comment.