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

View file

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

26
nasg.py
View file

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

104
shared.py
View file

@ -93,65 +93,42 @@ class XRay(CMDLine):
return json.loads(stdout.decode('utf-8').strip()) return json.loads(stdout.decode('utf-8').strip())
class Pandoc(CMDLine): class PandocNG(CMDLine):
""" Pandoc command line call with piped in- and output """ """ 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') super().__init__('pandoc')
if True == md2html: self.raw = raw
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'
def convert(self, text): def _convert(self, i, o):
cmd = ( cmd = (
self.executable, self.executable,
'-o-', '-o-',
'--from=%s' % self.i, '--from=%s' % i,
'--to=%s' % self.o '--to=%s' % o
) )
logging.debug('converting string with Pandoc') logging.debug('converting string with Pandoc')
p = subprocess.Popen( p = subprocess.Popen(
@ -161,7 +138,7 @@ class Pandoc(CMDLine):
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) )
stdout, stderr = p.communicate(input=text.encode()) stdout, stderr = p.communicate(input=self.raw.encode())
if stderr: if stderr:
logging.error( logging.error(
"Error during pandoc covert:\n\t%s\n\t%s", "Error during pandoc covert:\n\t%s\n\t%s",
@ -170,6 +147,27 @@ class Pandoc(CMDLine):
) )
return stdout.decode('utf-8').strip() 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): class ExifTool(CMDLine):
def __init__(self, fpath): def __init__(self, fpath):