Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
""" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')] |