diff --git a/requirements.txt b/requirements.txt index 75747ff..21d3478 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ spacy -weasyprint \ No newline at end of file +#weasyprint +flask diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripts/app.py b/scripts/app.py index 2fe13f2..3fd64da 100644 --- a/scripts/app.py +++ b/scripts/app.py @@ -2,10 +2,12 @@ from flask import Flask, render_template, request, Response # from weasyprint import HTML -from pagedjs import make_pdf +from .pagedjs import make_pdf +from .settings import DEBUG, BASEURL +# Spacy tries to import CUDA, do not break when it fails try: - from paseo import crear_camino + from .paseo import crear_camino except ModuleNotFoundError: pass @@ -13,9 +15,11 @@ import os.path basepath = os.path.dirname(__file__) -BASEURL = '' app = Flask(__name__) +# Book HTML is loaded through filesystem, in a tmp dir, make path absolute. +PAGEDJS_STATIC_DIR = os.path.join(basepath, 'static') + @app.route('{}/'.format(BASEURL)) def index(): return render_template('index.html') @@ -28,28 +32,34 @@ def book(): if fragment == 0: novel = os.path.join(basepath, '../data/emilia_prueba.txt') - author = 'Emilia Pardo Bazán' - title = 'La Madre Naturaleza' + author = 'Emilia Pardo Bazán' # Non breaking spaces + title = 'La Madre Naturaleza' # Non breaking spaces else: novel = os.path.join(basepath, '../data/prueba.txt') - author = 'Benito Pérez Gáldos' + author = 'Benito Pérez Gáldos' # Non breaking spaces title = 'Miau' path = crear_camino(novel, first_word) - context = { 'title': title, 'author': author, 'path': path } + context = { + 'title': title, + 'author': author, + 'path': path, + 'STATIC_DIR': '/static' if DEBUG else PAGEDJS_STATIC_DIR, + 'DEBUG': DEBUG + } + html = render_template('book.html', **context) + + if (DEBUG): + return html + else: + pdf = make_pdf(html) - # pdf = HTML(string=html).write_pdf() - - # Use pagedjs as weasyprint does not seem to support our layout. - pdf = make_pdf(html) - - r = Response(pdf, mimetype='application/pdf') + r = Response(pdf, mimetype='application/pdf') - r.headers.extend({ - 'Content-Disposition': 'attachment; filename="Paseo por arboles de madrid.pdf"' - }) + r.headers.extend({ + 'Content-Disposition': 'attachment; filename="Paseo por arboles de madrid.pdf"' + }) - return r - # return html + return r diff --git a/scripts/pagedjs.py b/scripts/pagedjs.py index 6fb6774..125c15a 100644 --- a/scripts/pagedjs.py +++ b/scripts/pagedjs.py @@ -1,13 +1,14 @@ import subprocess import tempfile import os.path +from .settings import PAGEDJS_BINARY_PATH + basepath = os.path.abspath(os.path.dirname(__file__)) -paged_bin = 'node_modules/pagedjs-cli/bin/paged' def run_pagedjs (path_html, path_pdf, cwd=None, extra_scripts=[]): args = [ - paged_bin + PAGEDJS_BINARY_PATH ] for script in extra_scripts: diff --git a/scripts/paseo.py b/scripts/paseo.py index e247c76..1ee733c 100644 --- a/scripts/paseo.py +++ b/scripts/paseo.py @@ -1,5 +1,5 @@ -from parse_trees import load_trees_from_json -from medialab import crear_base_datos, paso +from .parse_trees import load_trees_from_json +from .medialab import crear_base_datos, paso from random import shuffle, random # creating Markov Chain in text & trees diff --git a/scripts/settings.py b/scripts/settings.py new file mode 100644 index 0000000..09b1a89 --- /dev/null +++ b/scripts/settings.py @@ -0,0 +1,3 @@ +PAGEDJS_BINARY_PATH = 'node_modules/pagedjs-cli/bin/paged' +DEBUG = True +BASEURL = '' \ No newline at end of file diff --git a/scripts/static/MFI-Serreria/LICENSE b/scripts/static/MFI-Serreria/LICENSE new file mode 100644 index 0000000..9ff8244 --- /dev/null +++ b/scripts/static/MFI-Serreria/LICENSE @@ -0,0 +1,94 @@ +Copyright (c) 2014, the font authors (see font file information) +with Reserved Font Name MFI Serreria Extravagante. + +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. diff --git a/scripts/static/MFI-Serreria/MFI-Serreria-Extravagante.otf b/scripts/static/MFI-Serreria/MFI-Serreria-Extravagante.otf new file mode 100644 index 0000000..2dba8b6 Binary files /dev/null and b/scripts/static/MFI-Serreria/MFI-Serreria-Extravagante.otf differ diff --git a/scripts/static/MFI-Serreria/README.md b/scripts/static/MFI-Serreria/README.md new file mode 100644 index 0000000..433b884 --- /dev/null +++ b/scripts/static/MFI-Serreria/README.md @@ -0,0 +1,3 @@ +2012-2019 (c) Manufactura Independente & the participants of the "Stone to Spaceship" workshop at Medialab Prado in July 2012: Javier Gonzalez, Eduardo Merchán, Guillermo Afonso, Beatriz García, Alberto Hernández, Marta Calabria, Txus Tejado, Beatriz Fernández, Marcos Prack, Pablo Gámez, Miriam Castro, César A. Fernández, Gabriel Lucas, Alfredo Calosci, Rafael Parrilla. + +Visit http://manufacturaindependente.com/stonespaceship/ for additional information. diff --git a/scripts/static/PT_Serif/OFL.txt b/scripts/static/PT_Serif/OFL.txt new file mode 100644 index 0000000..164074b --- /dev/null +++ b/scripts/static/PT_Serif/OFL.txt @@ -0,0 +1,94 @@ +Copyright (c) 2010, ParaType Ltd. (http://www.paratype.com/public), +with Reserved Font Names "PT Sans", "PT Serif" and "ParaType". + +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. diff --git a/scripts/static/PT_Serif/PTSerif-Bold.ttf b/scripts/static/PT_Serif/PTSerif-Bold.ttf new file mode 100644 index 0000000..b955732 Binary files /dev/null and b/scripts/static/PT_Serif/PTSerif-Bold.ttf differ diff --git a/scripts/static/PT_Serif/PTSerif-BoldItalic.ttf b/scripts/static/PT_Serif/PTSerif-BoldItalic.ttf new file mode 100644 index 0000000..e933f84 Binary files /dev/null and b/scripts/static/PT_Serif/PTSerif-BoldItalic.ttf differ diff --git a/scripts/static/PT_Serif/PTSerif-Italic.ttf b/scripts/static/PT_Serif/PTSerif-Italic.ttf new file mode 100644 index 0000000..e53ac86 Binary files /dev/null and b/scripts/static/PT_Serif/PTSerif-Italic.ttf differ diff --git a/scripts/static/PT_Serif/PTSerif-Regular.ttf b/scripts/static/PT_Serif/PTSerif-Regular.ttf new file mode 100644 index 0000000..24af478 Binary files /dev/null and b/scripts/static/PT_Serif/PTSerif-Regular.ttf differ diff --git a/scripts/static/pagedjs-interface.css b/scripts/static/pagedjs-interface.css new file mode 100644 index 0000000..6cb2436 --- /dev/null +++ b/scripts/static/pagedjs-interface.css @@ -0,0 +1,3 @@ + + +/* CSS for Paged.js interface – v0.2 */ /* Change the look */ :root { --color-background: whitesmoke; --color-pageSheet: #cfcfcf; --color-pageBox: violet; --color-paper: white; --color-marginBox: transparent; --pagedjs-crop-color: black; --pagedjs-crop-shadow: white; --pagedjs-crop-stroke: 1px; } /* To define how the book look on the screen: */ @media screen { body { background-color: var(--color-background); } .pagedjs_pages { display: flex; width: calc(var(--pagedjs-width) * 2); flex: 0; flex-wrap: wrap; margin: 0 auto; } .pagedjs_page { background-color: var(--color-paper); box-shadow: 0 0 0 1px var(--color-pageSheet); margin: 0; flex-shrink: 0; flex-grow: 0; margin-top: 10mm; } .pagedjs_first_page { margin-left: var(--pagedjs-width); } .pagedjs_page:last-of-type { margin-bottom: 10mm; } .pagedjs_pagebox{ box-shadow: 0 0 0 1px var(--color-pageBox); } .pagedjs_left_page{ z-index: 20; width: calc(var(--pagedjs-bleed-left) + var(--pagedjs-pagebox-width))!important; } .pagedjs_left_page .pagedjs_bleed-right .pagedjs_marks-crop { border-color: transparent; } .pagedjs_left_page .pagedjs_bleed-right .pagedjs_marks-middle{ width: 0; } .pagedjs_right_page{ z-index: 10; position: relative; left: calc(var(--pagedjs-bleed-left)*-1); } /* show the margin-box */ .pagedjs_margin-top-left-corner-holder, .pagedjs_margin-top, .pagedjs_margin-top-left, .pagedjs_margin-top-center, .pagedjs_margin-top-right, .pagedjs_margin-top-right-corner-holder, .pagedjs_margin-bottom-left-corner-holder, .pagedjs_margin-bottom, .pagedjs_margin-bottom-left, .pagedjs_margin-bottom-center, .pagedjs_margin-bottom-right, .pagedjs_margin-bottom-right-corner-holder, .pagedjs_margin-right, .pagedjs_margin-right-top, .pagedjs_margin-right-middle, .pagedjs_margin-right-bottom, .pagedjs_margin-left, .pagedjs_margin-left-top, .pagedjs_margin-left-middle, .pagedjs_margin-left-bottom { box-shadow: 0 0 0 1px inset var(--color-marginBox); } /* uncomment this part for recto/verso book : ------------------------------------ */ /* .pagedjs_pages { flex-direction: column; width: 100%; } .pagedjs_first_page { margin-left: 0; } .pagedjs_page { margin: 0 auto; margin-top: 10mm; } .pagedjs_left_page{ width: calc(var(--pagedjs-bleed-left) + var(--pagedjs-pagebox-width) + var(--pagedjs-bleed-left))!important; } .pagedjs_left_page .pagedjs_bleed-right .pagedjs_marks-crop{ border-color: var(--pagedjs-crop-color); } .pagedjs_left_page .pagedjs_bleed-right .pagedjs_marks-middle{ width: var(--pagedjs-cross-size)!important; } .pagedjs_right_page{ left: 0; } */ /*--------------------------------------------------------------------------------------*/ /* uncomment this par to see the baseline : -------------------------------------------*/ /* .pagedjs_pagebox { --pagedjs-baseline: 22px; --pagedjs-baseline-position: 5px; --pagedjs-baseline-color: cyan; background: linear-gradient(transparent 0%, transparent calc(var(--pagedjs-baseline) - 1px), var(--pagedjs-baseline-color) calc(var(--pagedjs-baseline) - 1px), var(--pagedjs-baseline-color) var(--pagedjs-baseline)), transparent; background-size: 100% var(--pagedjs-baseline); background-repeat: repeat-y; background-position-y: var(--pagedjs-baseline-position); } */ /*--------------------------------------------------------------------------------------*/ } /* Marks (to delete when merge in paged.js) */ .pagedjs_marks-crop{ z-index: 999999999999; } .pagedjs_bleed-top .pagedjs_marks-crop, .pagedjs_bleed-bottom .pagedjs_marks-crop{ box-shadow: 1px 0px 0px 0px var(--pagedjs-crop-shadow); } .pagedjs_bleed-top .pagedjs_marks-crop:last-child, .pagedjs_bleed-bottom .pagedjs_marks-crop:last-child{ box-shadow: -1px 0px 0px 0px var(--pagedjs-crop-shadow); } .pagedjs_bleed-left .pagedjs_marks-crop, .pagedjs_bleed-right .pagedjs_marks-crop{ box-shadow: 0px 1px 0px 0px var(--pagedjs-crop-shadow); } .pagedjs_bleed-left .pagedjs_marks-crop:last-child, .pagedjs_bleed-right .pagedjs_marks-crop:last-child{ box-shadow: 0px -1px 0px 0px var(--pagedjs-crop-shadow); } \ No newline at end of file diff --git a/scripts/templates/book.html b/scripts/templates/book.html index 872013c..d5eff1e 100644 --- a/scripts/templates/book.html +++ b/scripts/templates/book.html @@ -6,37 +6,35 @@ Document + {% if DEBUG %} + + + {% endif %} -

Paseo por los árboles de Madrid con {{ author }} y {{ title }}

+

Paseo por los árboles de Madrid con {{ author }} y {{ title }}

{% for sentence, previous_steps, tree, traces in path %}
{{ previous_steps }}
- +
+ +
{{ tree.properties.NOMBRE_COMUN }} en {{ tree.properties.MINTDIRECCIONAUX }}
diff --git a/scripts/templates/index.html b/scripts/templates/index.html index 0a7b014..a9128c6 100644 --- a/scripts/templates/index.html +++ b/scripts/templates/index.html @@ -6,20 +6,76 @@ Paseo por los árboles de Madrid -

Paseo por los árboles de Madrid

-

En este libro, el algoritmo de las cadenas de Markov genera simultáneamente un poema y un paseo por los árboles del barrio de Las Letras, en el centro de Madrid. A pesar de la impresión de que hay pocos árboles en el barrio, el algoritmo cuenta con 460 de ellos.

-

La cadena de Markov fue diseñada en 1906 por Andrey Markov, un matemático ruso fallecido en 1992. Este algoritmo está en la base de muchos programas informáticos que generan spam. Se utiliza para sistemas que describen una serie de eventos que son interdependientes. Lo que ocurre depende únicamente del paso anterior.

-

El libro se construye entonces paso a paso.

-

- Puedes elegir una novela para crear tu Paseo por árboles de Madrid. -

-
-
-
- -
+

Paseo por los árboles de Madrid

+
+

En este libro, el algoritmo de las cadenas de Markov genera simultáneamente un poema y un paseo por los árboles del barrio de Las Letras, en el centro de Madrid. A pesar de la impresión de que hay pocos árboles en el barrio, el algoritmo cuenta con 460 de ellos.

+

La cadena de Markov fue diseñada en 1906 por Andrey Markov, un matemático ruso fallecido en 1992. Este algoritmo está en la base de muchos programas informáticos que generan spam. Se utiliza para sistemas que describen una serie de eventos que son interdependientes. Lo que ocurre depende únicamente del paso anterior.

+

El libro se construye entonces paso a paso.

+
+
+

+ Puedes elegir una novela para crear tu Paseo por árboles de Madrid. +

+
+
+
+ +
+