{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from weasyprint import HTML, CSS\n",
    "from weasyprint.fonts import FontConfiguration\n",
    "import nltk\n",
    "\n",
    "font_config = FontConfiguration()\n",
    "\n",
    "txt = open('txt/practicalvision.txt').read()\n",
    "words = nltk.word_tokenize(txt) #tokenizing the text\n",
    "tagged_words = nltk.pos_tag(words) #generating grammar tags for the tokens\n",
    "\n",
    "content = ''\n",
    "content += '<h1>Practical Vision, by Jalada</h1>'\n",
    "\n",
    "\n",
    "for word, tag in tagged_words:\n",
    "    content += f'<span class=\"{tag}\">{word}</span> ' #for every word, generate an html tag wich includes the grammar tag as class\n",
    "    if '.' in word:\n",
    "        content += '<br> \\n'\n",
    "\n",
    "with open(\"txt/practical_viz.html\", \"w\") as f: #save as html\n",
    "    f.write(f\"\"\"<!DOCTYPE html>\n",
    "<html>\n",
    "<head>\n",
    "    <meta charset=\"utf-8\">\n",
    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"grammar_viz.css\">\n",
    "    <title></title>\n",
    "</head>\n",
    "<body>\n",
    "{content}\n",
    "</body>\n",
    "\"\"\")\n",
    "\n",
    "html = HTML(\"txt/practical_viz.html\") #define as HTML the genereted html file\n",
    "\n",
    "css = CSS(string=''' \n",
    "    body{\n",
    "        size: A4;\n",
    "        font-family: serif;\n",
    "        font-size: 12pt;\n",
    "        line-height: 1.4;\n",
    "        padding: 3vw;\n",
    "        color: rgba(0,0,0,0)\n",
    "    }\n",
    "    h1{\n",
    "        width: 100%;\n",
    "        text-align: center;\n",
    "        font-size: 250%;\n",
    "        line-height: 1.25;\n",
    "        color: black;\n",
    "        \n",
    "    }''', font_config=font_config) #define CSS for print final A4 pdf\n",
    "\n",
    "html.write_pdf('practical_viz.pdf', stylesheets=[css], font_config=font_config) #generate A4 pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
