...
 
Showing with 592 additions and 1 deletions
......@@ -7,8 +7,31 @@ Structure of this folder:
> interface.html
> css
> stylesheet.css
> fonts
> fonts
> img
> js
> jquery-2.1.3.js
Requirements
============
* Jinja
* Weasyprint (for the export function)
Before running the cgi script
=============================
* the cgi-bin/interface.cgi needs to be executable
> chmod u+x cgi-bin/interface.cgi
* the same counts for cgi-bin/export.cgi
> chmod u+x cgi-bin/export.cgi
* the output folder needs to be writable
> chmod u+w output
Use
===
......
#!/usr/bin/env python
from __future__ import print_function
import cgi
import cgitb; cgitb.enable()
import os, sys
from datetime import datetime
# *******************************************************
# read input fields into variables
# *******************************************************
form = cgi.FieldStorage()
filename = form.getvalue("filename", "").decode('utf-8')
results = form.getvalue("results", "").decode('utf-8')
source = form.getvalue("source", "").decode('utf-8')
title = form.getvalue("title", "").decode('utf-8')
source = form.getvalue("source", "").decode('utf-8')
author = form.getvalue("author", "").decode('utf-8')
print('> printing ', filename, file=sys.stderr)
date = datetime.now().strftime('%d %B %Y, at %H:%M:%S')
# ****************
# export
# ****************
def save(filename):
# ***********************
# HTML > posted via AJAX
# ***********************
html_out = 'output/html/'+filename+'.html'
with open (html_out,'w+') as f:
html = '''<!DOCTYPE html>
<html class="page">
<head>
<meta charset="utf-8">
<title>'''+title+'''</title>
<link rel="stylesheet" type="text/css" href="../../css/stylesheet.css">
<link rel="stylesheet" type="text/css" href="../../css/export.css">
</head>
<body>
<div id="header">This document <b>'''+title+'''</b> is generated with the Algolit Extension CGI interface on <b>'''+date+'''</b>, and is written by the following author(s): <b>'''+author+'''</b>. It is based on the source '''+source+''' and set in the font <a href="https://github.com/belluzj/fantasque-sans" target="_blank">Fantasque</a>. Licenced under the <a href="http://artlibre.org/licence/lal/en/" target="_blank">Free Art licence</a>. </div>
<div id="wrapper">
'''+results+'''
</div>
</body>
</html>'''
f.write(html.encode('utf8'))
f.close()
print('> html file written', file=sys.stderr)
# *******************************************
# PDF with wkhtmltopdf: html > pdf
# *******************************************
pdf_out = 'output/pdf/'+filename+'.pdf'
cmd = "wkhtmltopdf "+html_out+' '+pdf_out
print('>>>', cmd, file=sys.stderr)
os.system(cmd)
print('> pdf generated', file=sys.stderr)
print('> done', file=sys.stderr)
if filename:
save(filename)
\ No newline at end of file
#!/usr/bin/env python
from __future__ import print_function
import os, sys
import cgi
import cgitb; cgitb.enable()
from jinja2 import FileSystemLoader, Environment
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize, TreebankWordTokenizer
from nltk.util import ngrams
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
# *******************************************************
# settings
# *******************************************************
path = "./"
env = Environment(loader=FileSystemLoader(path))
template = env.get_template("interface.html")
export = 'enabled'
# export = 'disabled'
# *******************************************************
# read form fields into variables
# *******************************************************
method = os.environ.get("REQUEST_METHOD")
form = cgi.FieldStorage()
title = form.getvalue("title", "").decode('utf-8')
input_text = form.getvalue("input_text", "").decode('utf-8')
# export settings
source = form.getvalue("source", "").decode('utf-8')
filename = form.getvalue("filename", "").decode('utf-8')
author = form.getlist('author')
if len(author) > 1:
', '.join(author)
# *******************************************************
# change variables (when form is submitted)
# *******************************************************
# for example:
lowercase = input_text.lower()
# *******************************************************
# set jinja variables
# *******************************************************
tvars = {}
tvars['method'] = method
tvars['title'] = title
tvars['input_text'] = input_text
# to use objects in the interface.html, define new variables here
# for example:
tvars['lowercase'] = lowercase
# export
tvars['export'] = export
tvars['filename'] = filename
tvars['source'] = source
tvars['author'] = author
# *******************************************************
# render
# *******************************************************
print('*variables rendered*\n', file=sys.stderr)
print("Content-type: text/html;charset=utf-8")
print()
html = template.render(tvars).encode('utf-8')
print(html)
\ No newline at end of file
@page{
width: 210mm;
height: 297mm;
margin:2mm 5mm 5mm 5mm;
zoom:1;
}
html.page body{
width: 100%;
}
html.page #wrapper{
width: 100%;
margin:0 auto;
left:0px;
}
html.page #header{
margin:0;
padding:0;
}
/* fontsizes */
html.page #header, html.page b{
font-family: monospace;
font-size: 8pt;
line-height: 1.4;
}
/* results */
html.page .result{
margin:0;
padding:0;
border:0;
width: 100%;
height: 173mm;
}
html.page b{
font-weight:bold;
margin:0;
padding:0;
}
Copyright (c) 2013-2016, Jany Belluz (jany.belluz@hotmail.fr)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
@font-face {
font-family: 'Fantasque Sans Mono';
src: url('FantasqueSansMono-Italic.eot'); /* IE 9 Compatibility Mode */
src: url('FantasqueSansMono-Italic.eot?#iefix') format('embedded-opentype'), /* IE < 9 */
url('FantasqueSansMono-Italic.woff2') format('woff2'),
url('FantasqueSansMono-Italic.woff') format('woff'), /* Firefox >= 3.6, any other modern browser */
url('FantasqueSansMono-Italic.ttf') format('truetype'), /* Safari, Android, iOS */
url('FantasqueSansMono-Italic.svg#FantasqueSansMono-Italic') format('svg'); /* Chrome < 4, Legacy iOS */
font-weight: 400;
font-style: italic;
}
@font-face {
font-family: 'Fantasque Sans Mono';
src: url('FantasqueSansMono-Regular.eot'); /* IE 9 Compatibility Mode */
src: url('FantasqueSansMono-Regular.eot?#iefix') format('embedded-opentype'), /* IE < 9 */
url('FantasqueSansMono-Regular.woff2') format('woff2'),
url('FantasqueSansMono-Regular.woff') format('woff'), /* Firefox >= 3.6, any other modern browser */
url('FantasqueSansMono-Regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('FantasqueSansMono-Regular.svg#FantasqueSansMono-Regular') format('svg'); /* Chrome < 4, Legacy iOS */
font-weight: 400;
font-style: normal;
}
@font-face {
font-family: 'Fantasque Sans Mono';
src: url('FantasqueSansMono-BoldItalic.eot'); /* IE 9 Compatibility Mode */
src: url('FantasqueSansMono-BoldItalic.eot?#iefix') format('embedded-opentype'), /* IE < 9 */
url('FantasqueSansMono-BoldItalic.woff2') format('woff2'),
url('FantasqueSansMono-BoldItalic.woff') format('woff'), /* Firefox >= 3.6, any other modern browser */
url('FantasqueSansMono-BoldItalic.ttf') format('truetype'), /* Safari, Android, iOS */
url('FantasqueSansMono-BoldItalic.svg#FantasqueSansMono-BoldItalic') format('svg'); /* Chrome < 4, Legacy iOS */
font-weight: 700;
font-style: italic;
}
@font-face {
font-family: 'Fantasque Sans Mono';
src: url('FantasqueSansMono-Bold.eot'); /* IE 9 Compatibility Mode */
src: url('FantasqueSansMono-Bold.eot?#iefix') format('embedded-opentype'), /* IE < 9 */
url('FantasqueSansMono-Bold.woff2') format('woff2'),
url('FantasqueSansMono-Bold.woff') format('woff'), /* Firefox >= 3.6, any other modern browser */
url('FantasqueSansMono-Bold.ttf') format('truetype'), /* Safari, Android, iOS */
url('FantasqueSansMono-Bold.svg#FantasqueSansMono-Bold') format('svg'); /* Chrome < 4, Legacy iOS */
font-weight: 700;
font-style: normal;
}
@font-face {
font-family: 'Fantasque';
src: url('fonts/fantasque/FantasqueSansMono-Regular.ttf');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Fantasque';
src: url('fonts/fantasque/FantasqueSansMono-Italic.ttf');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'Fantasque';
src: url('fonts/fantasque/FantasqueSansMono-Bold.ttf');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'Fantasque';
src: url('fonts/fantasque/FantasqueSansMono-BoldItalic.ttf');
font-weight: bold;
font-style: italic;
}
body{
position: relative;
font-family: 'Fantasque';
font-size: 14pt;
line-height: 1.55;
margin:0;
padding:0;
top:0;
left:0;
}
#wrapper{
position: relative;
width: 100%;
width: 800px;
top:0;
left:0;
margin:1em auto 0 auto;
padding:0;
}
/********************/
/* general elements */
h1, h2{
font-size: 100%;
font-weight: normal;
}
h1{
text-decoration: underline;
}
h1#title{
font-size: 350%;
font-weight: bold;
text-decoration: none;
line-height: 1.0;
margin:0 0 1em 0;
}
h2{
font-style: italic;
text-decoration: none;
}
small, sup{
font-size: 12px;
}
textarea, input{
margin:5px;
}
input[type='text']{
width: 250px;
}
/********************/
/* input text */
textarea{
width: 100%;
}
/********************/
/* results */
#results{
margin:2em 0;
}
/********************/
/* export */
#export-wrapper{
position: relative;
width: 100%;
background-color: rgba(240,240,240,1);
margin:0;
padding:0.5em 0 2em 0;
}
#export-container{
width: 800px;
margin:0 auto;
}
img.loading{
display: none;
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="../css/stylesheet.css">
<meta Content-Type: text/html; charset="utf-8">
<script type="text/javascript" src="../js/jquery-2.1.3.js"></script>
</head>
<body>
<form action="" method="post">
<div id="wrapper">
<h1 id="title">
{% if title %}
{{ title }}
{% else %}
<input id="titled" type="text" name="title" value="Enter a title here">
{% endif%}
</h1>
<h1>Input</h1>
<textarea name="input_text" rows="20">{{ input_text }}</textarea>
<input type="submit" value="submit">
{% if method == 'POST'%}
<div id="results">
<h1>Results</h1>
<!-- print results here -->
<!-- for example -->
<div class="lowercase">{{ lowercase }}</div>
</div>
{% endif %}
</div>
{% if method == 'POST'%}
{% if export == 'enabled' %}
<div id="export-wrapper">
<div id="export-container">
<h1>Export</h1>
Export these results with the following credits.
<br>source: <input id="source" type="text" name="source" value="{% if source %}{{ source }}{% endif %}">,
<br>author(s):
{% if author %}
{% for a in author %}
<input class="author" type="text" name="author" value="{{a}}">
{% endfor %}
{% else %}
<input class="author" type="text" name="author">
{% endif %}
<span id="another" class="author"></span><button id="addauthor" class="export">add one more author</button>,
<br>filename: <input id="filename" type="text" name="filename" value="{{ filename }}">
<br><button id="export" name="pdf">.pdf</button><img src="../img/loading.gif" class="loading" id="pdf" />
<button id="export" name="html">.html</button><img src="../img/loading.gif" class="loading" id="html" />.
</div>
</div>
{% endif %}
{% endif %}
</form>
</body>
<script type="text/javascript">
$('button#addauthor').click(function(e){
e.preventDefault();
i = 1;
$('#another.author').append(', <input class="author" type="text" name="author" value=""> ');
i =+ 1;
});
$('button#export').click(function(e){
e.preventDefault();
var name = $(this).attr('name');
var cgi = 'export.cgi';
var results = $('#results').html();
var source = $('#source').val();
var filename = $('#filename').val();
var title = $('#titled').val();
author = '';
$('input.author').each(function(){
if(author != ''){
author = author + ', ' +($(this).val());
}
else{
author = $(this).val();
}
});
$.ajax({
url: cgi,
method: "post",
data: {
filename : filename,
title : title,
source : source,
author : author,
results : results
},
dataType: "html",
success: function(){
window.location.hash = 'export';
$('input#state').val('export');
if(name == 'html'){
var win = window.open('/output/html/'+filename+'.html', '_blank');
}
if(name == 'pdf'){
var win = window.open('/output/pdf/'+filename+'.pdf', '_blank');
}
if (win) {
win.focus();
} else {
alert('Please allow popups for this website');
}
}
})
});
</script>
</html>
\ No newline at end of file
This diff could not be displayed because it is too large.
<!DOCTYPE html>
<html class="page">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/stylesheet.css">
<link rel="stylesheet" type="text/css" href="../../css/export.css">
</head>
<body>
<div id="header">This document <b></b> is generated with the Algolit Extension CGI interface on <b>03 October 2017, at 15:12:21</b>, and is written by the following author(s): <b>mb</b>. It is based on the source source and set in the font <a href="https://github.com/belluzj/fantasque-sans" target="_blank">Fantasque</a>. Licenced under the <a href="http://artlibre.org/licence/lal/en/" target="_blank">Free Art licence</a>. </div>
<div id="wrapper">
<h1>Results</h1>
<!-- print results here -->
<!-- for example -->
<div class="lowercase">hey cristina!</div>
</div>
</body>
</html>
\ No newline at end of file