from domfilter import ApplyDOMFilters, DOMFilters
from fetch import CachingFetcher
-from glowfic import BesideIconLayout, BelowIconLayout, Layout
+from glowfic import BesideIconLayout, BelowIconLayout
from htmlfilter import ApplyHTMLFilters, HTMLFilters
from texfilter import ApplyTexFilters, TexFilters
from images import DiskImageStore
def spec_from_commandline_args() -> Iterator[Spec]:
args = _command_line_parser().parse_args()
texifier = PandocTexifier(args.pandoc or 'pandoc')
- layout: Layout
+ layout: bytes
if args.layout == 'below':
- layout = BelowIconLayout(texifier)
+ layout = BelowIconLayout
elif args.layout == 'beside':
- layout = BesideIconLayout(texifier)
+ layout = BesideIconLayout
else:
raise ValueError(f'Unknown layout: {args.layout}')
log = (lambda _: None) if args.quiet else _print_status
# Free Software Foundation, version 3.
-from abc import ABC, abstractmethod
from dataclasses import dataclass
import itertools
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
content)
-class Layout(ABC):
+def renderChunk(texifier: Texifier, chunk: Chunk) -> bytes:
+ return b''.join([
+ br'\glowhead{',
+ br'\glowicon{%s}' % chunk.icon.encode('UTF-8') if chunk.icon else b'',
+ b'}{',
+ texifier.texify(chunk.character) if chunk.character else b'',
+ b'}{',
+ texifier.texify(chunk.screen_name) if chunk.screen_name else b'',
+ b'}{',
+ texifier.texify(chunk.author) if chunk.author else b'',
+ b'}',
+ texifier.texify(chunk.content)])
- @abstractmethod
- def renderChunk(self, chunk: Chunk) -> bytes:
- raise NotImplementedError()
+ContentOnlyLayout = br'''
+\newcommand{\glowhead}[4]{}
+'''
-class ContentOnlyLayout(Layout):
- def __init__(self, texifier: Texifier) -> None:
- self._texifier = texifier
-
- def renderChunk(self, chunk: Chunk) -> bytes:
- return self._texifier.texify(chunk.content) + b'\n'
-
-
-class BelowIconLayout(Layout):
-
- def __init__(self, texifier: Texifier) -> None:
- self._texifier = texifier
-
- def renderChunk(self, chunk: Chunk) -> bytes:
- meta = []
- if chunk.icon:
- meta += [br'\glowicon{%s}' % chunk.icon.encode('UTF-8')]
- meta += [self._texifier.texify(x)
- for x in [chunk.character, chunk.screen_name, chunk.author]
- if x is not None]
-
- return br'''\wrapstuffclear
+BelowIconLayout = br'''
+\newcommand{\glowhead}[4]{\wrapstuffclear
\begin{wrapstuff}[l]
\fbox{
\begin{varwidth}{0.5\textwidth}
}\\*}
\vspace{-1em}
\begin{center}
-%s
+#1\ifnotempty
+{#1}{\\*}#2\ifnotempty
+{#2}{\\*}#3\ifnotempty
+{#3}{\\*}#4
\end{center}
\end{varwidth}
}
\strut
-\noindent %s
-''' % (
- br'\\*'.join(meta),
- self._texifier.texify(chunk.content))
+\noindent}'''
-class BesideIconLayout(Layout):
+# Why is \textwidth not the width of the text?
+# Why is the width of the text .765\textwidth?
+BesideIconLayout = br'''
+\newcommand{\glowhead}[4]{
- def __init__(self, texifier: Texifier) -> None:
- self._texifier = texifier
-
- def renderChunk(self, chunk: Chunk) -> bytes:
- meta = [
- chunk.character,
- chunk.screen_name,
- chunk.author,
- ]
+\strut
- # Why is \textwidth not the width of the text?
- # Why is the width of the text .765\textwidth?
- return br'''\noindent\fbox{
-%s
+\noindent\fbox{
+#1
\parbox[b]{.765\textwidth}{
\begin{center}
-%s
+#2\ifnotempty
+{#2}{\\*}#3\ifnotempty
+{#3}{\\*}#4
\end{center}
}
}\\*
\vspace{-0.75em}\\*
-\noindent %s
-
-\strut
-
-''' % (
- br'\glowicon{%s}' % chunk.icon.encode('UTF-8') if chunk.icon else b'',
- br'\\*'.join(self._texifier.texify(x) for x in meta if x is not None),
- self._texifier.texify(chunk.content))
+\noindent}'''
from bs4 import BeautifulSoup
from args import spec_from_commandline_args
-from glowfic import chunkDOMs, flatURL, makeChunk
+from glowfic import chunkDOMs, flatURL, makeChunk, renderChunk
from spec import Spec
\newcommand{\glowicon}[1]{\includegraphics[
width=\glowiconsize,height=\glowiconsize,keepaspectratio
]{#1}}
-''' % spec.icon_size)
+\newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
+ \detokenize{#1}\relax\else #2\fi}
+%s
+''' % (spec.icon_size, spec.layout))
url = flatURL(spec.url)
spec.log('Fetching HTML...\r')
html = spec.fetcher.fetch(url)
spec.log(f'Processing chunk {i} of {num_chunks} ({percent:.1f}%)\r')
spec.domfilter(r)
chunk = makeChunk(r, spec.images)
- spec.texout.write(spec.texfilter(spec.layout.renderChunk(chunk)))
+ spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
spec.log('')
spec.texout.write(b'\\end{document}\n')
texifier,
lambda x: x,
20,
- ContentOnlyLayout(texifier),
+ ContentOnlyLayout,
'margin=20mm',
buf,
lambda _: None)
(\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry}
\\begin{document}
(.|\n)*
-This is \\href{https://glowfic.com}{glowfic}
-
-You \\emph{sure}\?
-
-Pretty sure.
-
+\\glowhead{}{}{}{}This is \\href{https://glowfic.com}{glowfic}
+\\glowhead{}{}{}{}You \\emph{sure}\?
+\\glowhead{}{}{}{}Pretty sure.
\\end{document}
''', buf.getvalue())
texifier,
lambda x: x,
20,
- ContentOnlyLayout(texifier),
+ ContentOnlyLayout,
None,
buf,
lambda _: None)
texifier,
lambda x: x,
20,
- BesideIconLayout(texifier),
+ BesideIconLayout,
None,
out,
lambda _: None)
from bs4.element import Tag
from fetch import Fetcher
-from glowfic import Layout
from images import ImageStore
from texify import Texifier
texifier: Texifier
texfilter: Callable[[bytes], bytes]
icon_size: float
- layout: Layout
+ layout: bytes
geometry: str | None
texout: IO[bytes]
log: Callable[[str], None]