new pandoc function with a bit more sanity on usage and autopep cleanup

This commit is contained in:
Peter Molnar 2018-06-29 11:40:22 +02:00
parent 0f7eb328c0
commit 49bcb80e27
4 changed files with 77 additions and 72 deletions

View file

@ -209,6 +209,7 @@ class FlickrFavs(Favs):
fav.run()
# fav.fix_extension()
class TumblrFavs(Favs):
url = 'https://api.tumblr.com/v2/user/likes'
@ -516,9 +517,9 @@ class FlickrFav(ImgFav):
arrow.utcnow().timestamp
)
),
'title': '%s' % shared.Pandoc('plain').convert(
'title': '%s' % shared.PandocNG(
self.photo.get('title', '')
).rstrip(),
).txt.rstrip(),
'favorite-of': self.url,
'tags': self.photo.get('tags', '').split(' '),
'geo': {
@ -533,9 +534,9 @@ class FlickrFav(ImgFav):
},
}
self.content = shared.Pandoc('plain').convert(
self.content = shared.PandocNG(
self.photo.get('description', {}).get('_content', '')
)
).txt
self.fix_extension()
self.write_exif()
@ -581,7 +582,9 @@ class DAFav(ImgFav):
arrow.utcnow().timestamp
)
),
'title': '%s' % shared.Pandoc('plain').convert(self.title).rstrip(),
'title': '%s' % shared.PandocNG(
self.title
).txt.rstrip(),
'favorite-of': self.url,
'tags': [t.get('tag_name') for t in self.fav.get('meta', {}).get('tags', [])],
'author': {
@ -590,7 +593,7 @@ class DAFav(ImgFav):
},
}
c = "%s" % self.fav.get('meta', {}).get('description', '')
self.content = shared.Pandoc('plain').convert(c)
self.content = shared.PandocNG(c).txt
self.fix_extension()
self.write_exif()

View file

@ -29,7 +29,7 @@ import os
import re
import smtplib
import logging
from shared import Pandoc
from shared import PandocNG
class Letter(object):
@ -54,7 +54,7 @@ class Letter(object):
@property
def _html(self):
return Pandoc().convert(self.text)
return PandocNG(self.text).html
@property
def _tmpl(self):

26
nasg.py
View file

@ -543,7 +543,7 @@ class Singular(object):
@property
def is_old(self):
tdiff = arrow.utcnow() - arrow.get(self.mtime)
return (tdiff.days > 2*365)
return (tdiff.days > 2 * 365)
@property
def htmlfile(self):
@ -729,9 +729,8 @@ class Singular(object):
@property
def html(self):
html = "%s" % (self.body)
return shared.Pandoc().convert(html)
# return shared.Pandoc().convert(html)
return shared.PandocNG("%s" % (self.body)).html
@property
def title(self):
@ -752,7 +751,9 @@ class Singular(object):
if not s:
return s
if not hasattr(self, '_summary'):
self._summary = shared.Pandoc().convert(s)
#self._summary = shared.Pandoc().convert(s)
self._summary = shared.PandocNG(s).html
return self._summary
@property
@ -1162,11 +1163,11 @@ class WebImage(object):
# 2.39 is the wide angle cinematic view: anything wider, than that
# is panorama land
if ratio > 2.4 and not crop:
size = int(size*0.6)
size = int(size * 0.6)
horizontal = not horizontal
if (horizontal and not crop) \
or (not horizontal and crop):
or (not horizontal and crop):
w = size
h = int(float(size / width) * height)
else:
@ -1310,8 +1311,9 @@ class Comment(object):
@property
def html(self):
html = "%s" % (self.content)
return shared.Pandoc().convert(html)
#html = "%s" % (self.content)
# return shared.Pandoc().convert(html)
return shared.PandocNG("%s" % (self.content)).html
@property
def target(self):
@ -1351,7 +1353,8 @@ class Comment(object):
self._type = ''
if len(self.content):
maybe = shared.Pandoc('plain').convert(self.content)
#maybe = shared.Pandoc('plain').convert(self.content)
maybe = shared.PandocNG(self.content).txt
if maybe in UNICODE_EMOJI:
self._type = maybe
return self._type
@ -1507,7 +1510,8 @@ class Webmention(object):
what = self._source.get('data').get('content').get('text')
else:
return ''
return shared.Pandoc('html').convert(what)
# return shared.Pandoc('html').convert(what)
return shared.PandocNG(what).html
@property
def fname(self):

104
shared.py
View file

@ -93,65 +93,42 @@ class XRay(CMDLine):
return json.loads(stdout.decode('utf-8').strip())
class Pandoc(CMDLine):
class PandocNG(CMDLine):
""" Pandoc command line call with piped in- and output """
i_md = "markdown+" + "+".join([
'backtick_code_blocks',
'auto_identifiers',
'fenced_code_attributes',
'definition_lists',
'grid_tables',
'pipe_tables',
'strikeout',
'superscript',
'subscript',
'markdown_in_html_blocks',
'shortcut_reference_links',
'autolink_bare_uris',
'raw_html',
'link_attributes',
'header_attributes',
'footnotes',
])
o_md = "markdown-" + "-".join([
'raw_html',
'native_divs',
'native_spans',
])
def __init__(self, md2html=True):
def __init__(self, raw):
super().__init__('pandoc')
if True == md2html:
self.i = "markdown+" + "+".join([
'backtick_code_blocks',
'auto_identifiers',
'fenced_code_attributes',
'definition_lists',
'grid_tables',
'pipe_tables',
'strikeout',
'superscript',
'subscript',
'markdown_in_html_blocks',
'shortcut_reference_links',
'autolink_bare_uris',
'raw_html',
'link_attributes',
'header_attributes',
'footnotes',
])
self.o = 'html5'
elif 'plain' == md2html:
self.i = "markdown+" + "+".join([
'backtick_code_blocks',
'auto_identifiers',
'fenced_code_attributes',
'definition_lists',
'grid_tables',
'pipe_tables',
'strikeout',
'superscript',
'subscript',
'markdown_in_html_blocks',
'shortcut_reference_links',
'autolink_bare_uris',
'raw_html',
'link_attributes',
'header_attributes',
'footnotes',
])
self.o = "plain"
else:
self.o = "markdown-" + "-".join([
'raw_html',
'native_divs',
'native_spans',
])
self.i = 'html'
self.raw = raw
def convert(self, text):
def _convert(self, i, o):
cmd = (
self.executable,
'-o-',
'--from=%s' % self.i,
'--to=%s' % self.o
'--from=%s' % i,
'--to=%s' % o
)
logging.debug('converting string with Pandoc')
p = subprocess.Popen(
@ -161,7 +138,7 @@ class Pandoc(CMDLine):
stderr=subprocess.PIPE,
)
stdout, stderr = p.communicate(input=text.encode())
stdout, stderr = p.communicate(input=self.raw.encode())
if stderr:
logging.error(
"Error during pandoc covert:\n\t%s\n\t%s",
@ -170,6 +147,27 @@ class Pandoc(CMDLine):
)
return stdout.decode('utf-8').strip()
@property
def html(self):
return self._convert(
i=self.i_md,
o="html5"
)
@property
def md(self):
return self._convert(
i="html5",
o=self.o_md
)
@property
def txt(self):
return self._convert(
i=self.i_md,
o="plain"
)
class ExifTool(CMDLine):
def __init__(self, fpath):