all repos — nasg @ 49bcb80e27f1a17c3d75f6c1dd866bc8f73c027d

new pandoc function with a bit more sanity on usage and autopep cleanup
Peter Molnar hello@petermolnar.eu
Fri, 29 Jun 2018 11:40:22 +0200
commit

49bcb80e27f1a17c3d75f6c1dd866bc8f73c027d

parent

0f7eb328c0056a5f5bdb9fb1725884608ab04668

4 files changed, 77 insertions(+), 72 deletions(-)

jump to
M archive.pyarchive.py

@@ -209,6 +209,7 @@ if not fav.exists:

fav.run() # fav.fix_extension() + class TumblrFavs(Favs): url = 'https://api.tumblr.com/v2/user/likes'

@@ -516,9 +517,9 @@ self.photo.get('date_faved',

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 @@ ),

}, } - 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 @@ self.fav.get('published_time',

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 @@ 'url': 'https://%s.deviantart.com' % (self.author),

}, } 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()
M envelope.pyenvelope.py

@@ -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 @@ self.headers = {}

@property def _html(self): - return Pandoc().convert(self.text) + return PandocNG(self.text).html @property def _tmpl(self):
M nasg.pynasg.py

@@ -543,7 +543,7 @@

@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 @@ return body

@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 @@ s = self.meta.get('summary', '')

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 @@ # the actual limit

# 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 @@ return arrow.get(self.meta.get('date'))

@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 @@ if t != 'webmention':

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 @@ elif 'text' in self._source.get('data').get('content'):

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):
M shared.pyshared.py

@@ -93,65 +93,42 @@

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 @@ stdout=subprocess.PIPE,

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",

@@ -169,6 +146,27 @@ cmd,

stderr ) 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):