def render(template, data, weld=True, layout='layout'): with open('./solder/templates/%s.html' % template) as f: t = PyQuery(f.read()) t_body = t('body') if weld: if 'welds' in data: welds = data['welds'] if len(welds): for weld in welds: t(weld[0]).weld(weld[1]) if 'sources' in data: sources = data['sources'] script = t('<script id="welds" type="text/javascript"></script>') text = """ jQuery(document).ready(function($) { """ for source in sources: text += "$('%s').solder('%s');" % source text += """ }); """ script.text(text) t_body.append(script) if layout is not None: with open('./solder/templates/%s.html' % layout) as f: l = PyQuery(f.read()) l('head').append(t('head *')) l_body = l('body') for name, value in t_body[0].items(): if name == 'class': l_body.addClass(value) else: l_body.attr(name, value) html = t_body.html() if html: l('#content').html(html) t = l return t.__html__()
def linebreaks(self, content): """Converts newlines into <p> and <br />s.""" # Separate h1 ~ h6, ul, and ol so they won't go into <p> tag for e in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol']: opening_tag = '<{}>'.format(e) closing_tag = '</{}>'.format(e) content = content.replace(opening_tag, '\n\n{}'.format(opening_tag)) content = content.replace(closing_tag, '{}\n\n'.format(closing_tag)) # Remove <br /> after ol, ul, and li d = PyQuery(linebreaks(content)) for e in ['ol+br', 'ol>br', 'ul+br', 'ul>br', 'li+br']: d(e).remove() # Remove height attribute of image for e in d('img'): if 'height' in e.attrib: del e.attrib['height'] # Remove `content` class that breaks entries' styling d('div.content').removeClass('content') return d.__html__()