Skip to content
Permalink
Browse files

new forms plugin

  • Loading branch information
ralsina committed May 23, 2015
1 parent 2a5db98 commit 49ebb6838a7eafb53406c6fce110711897422df1
Showing with 116 additions and 0 deletions.
  1. +57 −0 v7/forms/README.md
  2. +10 −0 v7/forms/forms.plugin
  3. +49 −0 v7/forms/forms.py
@@ -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>
"""
```
@@ -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.

@@ -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.
You can’t perform that action at this time.