Improve book reports
This commit is contained in:
parent
b6e8167e2e
commit
bcbb7406f1
|
@ -0,0 +1,25 @@
|
||||||
|
\NeedsTeXFormat{LaTeX2e}
|
||||||
|
\ProvidesClass{bookreport}[2022/3/12 Book Report]
|
||||||
|
|
||||||
|
\LoadClass{article}
|
||||||
|
|
||||||
|
\usepackage{iflang}
|
||||||
|
\usepackage[margin=1in]{geometry}
|
||||||
|
|
||||||
|
\renewcommand{\maketitle}{
|
||||||
|
\IfLanguageName{portuguese}
|
||||||
|
{
|
||||||
|
\noindent\large
|
||||||
|
Título: \hfill \@title\\
|
||||||
|
Autor: \hfill \@author\\
|
||||||
|
Data da Ficha: \hfill \@date\\
|
||||||
|
\rule{\textwidth}{0.4pt}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
\noindent\large
|
||||||
|
Title: \hfill \@title\\
|
||||||
|
Author: \hfill \@author\\
|
||||||
|
Report Date: \hfill \@date\\
|
||||||
|
\rule{\textwidth}{0.4pt}
|
||||||
|
}
|
||||||
|
}
|
56
generate.py
56
generate.py
|
@ -7,7 +7,11 @@ from html.parser import HTMLParser
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
from pygments.lexers import get_lexer_by_name
|
from pygments.lexers import get_lexer_by_name
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if len(sys.argv) > 1 and 'clean' not in sys.argv:
|
||||||
|
print('usage: %s [clean]' % sys.argv[0])
|
||||||
|
exit(1)
|
||||||
|
|
||||||
class CodeHighlighter(HTMLParser):
|
class CodeHighlighter(HTMLParser):
|
||||||
data = ''
|
data = ''
|
||||||
|
@ -64,7 +68,10 @@ outroot = 'out'
|
||||||
output = 'Files'
|
output = 'Files'
|
||||||
templates = 'templates'
|
templates = 'templates'
|
||||||
|
|
||||||
|
if 'clean' in sys.argv:
|
||||||
shutil.rmtree(outroot, ignore_errors=True)
|
shutil.rmtree(outroot, ignore_errors=True)
|
||||||
|
|
||||||
|
try:
|
||||||
os.mkdir(outroot)
|
os.mkdir(outroot)
|
||||||
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
|
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
|
||||||
shutil.copy(templates + '/highlight.css', outroot + '/highlight.css')
|
shutil.copy(templates + '/highlight.css', outroot + '/highlight.css')
|
||||||
|
@ -72,6 +79,8 @@ shutil.copy(templates + '/cabinet.png', outroot + '/cabinet.png')
|
||||||
shutil.copy(templates + '/jquery.js', outroot + '/jquery.js')
|
shutil.copy(templates + '/jquery.js', outroot + '/jquery.js')
|
||||||
shutil.copytree(templates + '/mathjax', outroot + '/mathjax')
|
shutil.copytree(templates + '/mathjax', outroot + '/mathjax')
|
||||||
shutil.copytree(templates + '/bootstrap', outroot + '/bootstrap')
|
shutil.copytree(templates + '/bootstrap', outroot + '/bootstrap')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
with open(templates + '/file.html', 'r') as template:
|
with open(templates + '/file.html', 'r') as template:
|
||||||
|
@ -101,8 +110,44 @@ class File:
|
||||||
self.root_reference = re.sub(r'.+?/', '../', outdir)
|
self.root_reference = re.sub(r'.+?/', '../', outdir)
|
||||||
self.root_reference = re.sub(r'/[^\.]+$', '/', self.root_reference)
|
self.root_reference = re.sub(r'/[^\.]+$', '/', self.root_reference)
|
||||||
|
|
||||||
self.content = subprocess.check_output(['pandoc', '--mathjax=templates/mathjax/es5/tex-mml-chtml.js', '-f', 'latex', '-t', 'html',
|
path = '%s/%s' % (root, name)
|
||||||
'%s/%s' % (root, name)]).decode()
|
with open(path, 'r') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
m = re.findall(r'\\selectlanguage\{(.*?)\}', content)
|
||||||
|
if not m:
|
||||||
|
m = re.findall(r'\\usepackage\[(.*?)\]\{babel\}', content)
|
||||||
|
if not m:
|
||||||
|
m = re.findall(r'\\documentclass\[(.*?)\]\{.*\}', content)
|
||||||
|
lang = m[0] if len(m) > 0 else 'english'
|
||||||
|
|
||||||
|
m = re.findall(r'\\documentclass\{(.*?)\}', content)
|
||||||
|
doc_class = m[0] if len(m) > 0 else 'article'
|
||||||
|
|
||||||
|
options = [
|
||||||
|
'pandoc',
|
||||||
|
'--mathjax=templates/mathjax/es5/tex-mml-chtml.js',
|
||||||
|
'-f', 'latex',
|
||||||
|
'-t', 'html',
|
||||||
|
path
|
||||||
|
]
|
||||||
|
|
||||||
|
if doc_class == 'bookreport':
|
||||||
|
options.append('-s')
|
||||||
|
options.append('--template')
|
||||||
|
options.append('templates/default.html')
|
||||||
|
|
||||||
|
self.content = subprocess.check_output(options).decode()
|
||||||
|
|
||||||
|
if doc_class == 'bookreport':
|
||||||
|
if lang == 'portuguese':
|
||||||
|
self.content = re.sub(r'!\*\*title\*\*!', 'Título', self.content)
|
||||||
|
self.content = re.sub(r'!\*\*author\*\*!', 'Autor', self.content)
|
||||||
|
self.content = re.sub(r'!\*\*date\*\*!', 'Data da Ficha', self.content)
|
||||||
|
else:
|
||||||
|
self.content = re.sub(r'!\*\*title\*\*!', 'Title', self.content)
|
||||||
|
self.content = re.sub(r'!\*\*author\*\*!', 'Author', self.content)
|
||||||
|
self.content = re.sub(r'!\*\*date\*\*!', 'Report Date', self.content)
|
||||||
|
|
||||||
def expand_html(self):
|
def expand_html(self):
|
||||||
title = self.basename.replace('_', ' ')
|
title = self.basename.replace('_', ' ')
|
||||||
|
@ -125,12 +170,17 @@ class File:
|
||||||
f.write(html_content)
|
f.write(html_content)
|
||||||
|
|
||||||
def write_pdf(self):
|
def write_pdf(self):
|
||||||
subprocess.run(['latexmk', '-pdf', '-outdir=%s' % self.outdir, self.input_path])
|
subprocess.run(['latexmk', '-shell-escape', '-pdf', '-outdir=%s' % self.outdir, self.input_path])
|
||||||
|
|
||||||
subprocess.run(['latexmk', '-c', '-outdir=%s' % self.outdir, self.input_path])
|
subprocess.run(['latexmk', '-c', '-outdir=%s' % self.outdir, self.input_path])
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
|
html = self.outdir + '/' + self.html
|
||||||
|
pdf = self.outdir + '/' + self.pdf
|
||||||
|
input_time = os.path.getmtime(self.input_path)
|
||||||
|
if not os.path.isfile(html) or input_time > os.path.getmtime(html):
|
||||||
self.write_html()
|
self.write_html()
|
||||||
|
if not os.path.isfile(pdf) or input_time > os.path.getmtime(pdf):
|
||||||
self.write_pdf()
|
self.write_pdf()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<style>
|
||||||
|
#meta {
|
||||||
|
width: 100%;
|
||||||
|
font-size: 1.3em;
|
||||||
|
}
|
||||||
|
|
||||||
|
#meta td:nth-child(2) {
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<table id="meta">
|
||||||
|
<tr>
|
||||||
|
<td>!**title**!:</td> <td>$title$</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>!**author**!:</td> <td>$author$</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>!**date**!:</td> <td>$date$</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<article>
|
||||||
|
$body$
|
||||||
|
</article>
|
|
@ -73,6 +73,10 @@ main {
|
||||||
margin-bottom: 1rem;
|
margin-bottom: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.linenos {
|
||||||
|
width: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
.highlight {
|
.highlight {
|
||||||
background-color: #111;
|
background-color: #111;
|
||||||
padding: .2em .5em;
|
padding: .2em .5em;
|
||||||
|
|
Loading…
Reference in New Issue