Skip to content

Commit 49ebb68

Browse files
committedMay 23, 2015
new forms plugin
1 parent 2a5db98 commit 49ebb68

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
 

Diff for: ‎v7/forms/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
This plugin uses [Alpaca Forms](http://alpacajs.org/) to allow "easy" form creation
2+
in reStructuredText documents.
3+
4+
Here's an example:
5+
6+
```
7+
.. form::
8+
9+
{
10+
"schema": {
11+
"title": "What do you think of Alpaca?",
12+
"type": "object",
13+
"properties": {
14+
"name": {
15+
"type": "string",
16+
"title": "Name"
17+
},
18+
"ranking": {
19+
"type": "string",
20+
"title": "Ranking",
21+
"enum": ['excellent', 'not too shabby', 'alpaca built my hotrod']
22+
}
23+
}
24+
}
25+
```
26+
27+
Instead of using the form description as content for the directive, you can put it in a separate file
28+
and load it like this:
29+
30+
```
31+
.. form::
32+
:file: formdescription.json
33+
```
34+
35+
A description of how Alpaca works is beyond the scope of this README, and you should read
36+
[their fine docs](http://alpacajs.org/tutorial.html) instead.
37+
38+
You will probably want to add something like this to your config:
39+
40+
41+
```
42+
EXTRA_HEAD_DATA += """
43+
<link type="text/css" href="//code.cloudcms.com/alpaca/1.5.8/bootstrap/alpaca.min.css" rel="stylesheet" />
44+
"""
45+
46+
BODY_END += """
47+
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.js"></script>
48+
<script type="text/javascript" src="//code.cloudcms.com/alpaca/1.5.8/bootstrap/alpaca.min.js"></script>
49+
<script>
50+
$(document).ready(function() {
51+
$('div.alpacaform').each(function(index, element) {
52+
data = eval(element.id+'_data');
53+
Alpaca(element, data);
54+
});});
55+
</script>
56+
"""
57+
```

Diff for: ‎v7/forms/forms.plugin

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Core]
2+
Name = rest_forms
3+
Module = forms
4+
5+
[Documentation]
6+
Author = Roberto Alsina
7+
Version = 0.1
8+
Website = http://getnikola.com
9+
Description = Alpaca-based forms for ReST.
10+

Diff for: ‎v7/forms/forms.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
# This file is public domain according to its author, Brian Hsu
3+
4+
import uuid
5+
6+
from docutils.parsers.rst import Directive, directives
7+
from docutils import nodes
8+
9+
from nikola.plugin_categories import RestExtension
10+
11+
12+
class Plugin(RestExtension):
13+
14+
name = "rest_form"
15+
16+
def set_site(self, site):
17+
self.site = site
18+
directives.register_directive('form', AlpacaForms)
19+
return super(Plugin, self).set_site(site)
20+
21+
22+
class AlpacaForms(Directive):
23+
""" Embed AlpacaForm form
24+
25+
Usage:
26+
27+
.. form::
28+
29+
[Alpaca form description as JSON]
30+
31+
.. form::
32+
:file: formdescription.json
33+
34+
35+
"""
36+
37+
option_spec = {'file': directives.unchanged}
38+
has_content = True
39+
40+
def run(self):
41+
formname = 'form_'+uuid.uuid4().hex
42+
fname = self.options.get('file')
43+
if fname is None:
44+
data = '\n'.join(self.content)
45+
else:
46+
with open(fname, 'rb') as fd:
47+
data = fd.read()
48+
return [nodes.raw('', '''
49+
<div id={formname} class="alpacaform"></div> <script type="text/javascript"> {formname}_data={data};</script>'''.format(formname=formname, data=data), format='html')]

0 commit comments

Comments
 (0)
Please sign in to comment.