def js(location: str, asynchr: bool = False, defer: bool = False) -> str: """Get HTML <script> tags for a location """ location = util.escape_html(url(location)) asynchr = ' async' if asynchr else '' defer = ' defer' if defer else '' return '<script type="text/javascript" src="{}"{}{}></script>'.format( location, asynchr, defer)
def before(self) -> Optional[http.RedirectResponse]: if not auth.get_current_user().is_anonymous: return # Redirecting to the authorization endpoint inp = self.request.inp.copy() inp.update({ 'driver': _api.get_driver().name, '__redirect': util.escape_html(router.current_url(True)), }) return self.redirect(router.rule_url('auth_ui@sign_in', inp))
def get_responsive_html(self, alt: str = '', css: str = '', aspect_ratio: float = None, enlarge: bool = True) -> str: """Get HTML code to embed the image (responsive way). """ alt = _util.escape_html(alt) css += ' img-responsive img-fluid pytsite-img' return '<span class="{}" data-url="{}" data-alt="{}" data-aspect-ratio="{}" ' \ 'data-width="{}" data-height="{}" data-enlarge="{}"></span>' \ .format(css.strip(), self.url, alt, aspect_ratio, self.width, self.height, enlarge)
def export(self, entity, exporter): """Performs export. :type entity: plugins.content._model.Content :type exporter: plugins.content_export._model.ContentExport """ try: _logger.info("Export started. '{}'".format(entity.title)) tags = exporter.add_tags if entity.has_field('tags'): tags += tuple([tag.title for tag in entity.f_get('tags')]) opts = exporter.driver_opts msg = '' if entity.has_field('images') and entity.images: img_url = entity.images[0].get_url(width=1024) msg += '<p><a href="{}"><img src="{}" title="{}"></a></p>'. \ format(entity.url, img_url, _util.escape_html(entity.title)) msg += '<p>{}: <a href="{}">{}</a></p>'.format( _lang.t('content_export_livejournal@source', language=entity.language), entity.url, entity.url) if entity.has_field('description'): msg += '<p>{}</p>'.format(entity.f_get('description')) msg += '<lj-cut>' msg_body = entity.f_get('body', process_tags=True, responsive_images=False, images_width=1200) msg_body = msg_body.replace('\r', '').replace('\n', '') msg += _util.trim_str(msg_body, 64000, True) msg += '</lj-cut>' if opts['lj_like']: msg += '<lj-like buttons="{}">'.format(opts['lj_like']) s = _livejournal.Session(opts['username'], opts['password']) pub_time = entity.f_get('publish_time') if entity.has_field( 'publish_time') else entity.created r = s.post_event(entity.title[:255], msg, tags, pub_time) _logger.info("Export finished. '{}'. LJ response: {}".format( entity.title, r)) except Exception as e: raise _content_export.error.ExportError(e)
def t_set(tag: str, value: str = None, **kwargs): """Set tag's value """ if not _tags: raise RuntimeError('reset() should be called before') tid = _threading.get_id() if tag not in _tags[tid]: _tags[tid][tag] = [] if tag == 'link' else '' if tag == 'link': _tags[tid][tag].append(kwargs) else: _tags[tid][tag] = _util.escape_html(value)
def odm_ui_browser_row(self) -> tuple: model = _content.get_model_title(self.content_model) driver = _api.get_driver(self.driver).get_description() driver_options = str(dict(self.driver_opts)) content_section = self.content_section.title if self.content_section else ' ' content_author = self.content_author.first_last_name enabled = '<span class="label label-success">' + self.t('word_yes') + '</span>' if self.enabled else '' paused_till = self.f_get('paused_till', fmt='pretty_date_time') if _datetime.now() < self.paused_till else '' if self.errors: errors = '<span class="label label-danger" title="{}">{}</span>' \ .format(_util.escape_html(self.last_error), self.errors) else: errors = '' return model, driver, driver_options, content_section, content_author, enabled, errors, paused_till
def get_html(self, alt: str = '', css: str = '', width: int = 0, height: int = 0, enlarge: bool = True): """Get HTML code to embed the image. """ if not enlarge: if width and width > self.width: width = self.width if height and height > self.height: height = self.height css += ' img-responsive' return '<img src="{}" class="{}" alt="{}">'.format( self.get_url(width=width, height=height), css.strip(), _util.escape_html(alt))
def css(location: str) -> str: """Get HTML <link rel="stylesheet"> tag for a location """ return '<link rel="stylesheet" href="{}">'.format( util.escape_html(url(location)))
def process_img_tag(match): """Converts single body [img] tag into HTML <img> tag """ nonlocal responsive_images, images_width # Image index img_index = int(match.group(1)) # Does image exist? if entity_images_count < img_index: return '' img = entity_images[img_index - 1] # Additional parameters defaults link_orig = False link_target = '_blank' link_class = '' img_css = '' enlarge = enlarge_images_setting alt = entity.title if entity.has_field('title') else '' width = 0 height = 0 responsive = responsive_images for arg in match.group(2).split(':'): # type: str arg = arg.strip() if arg in ('link_orig', 'link'): link_orig = True elif arg.startswith('link_target='): link_target = arg.split('=')[1] elif arg.startswith('link_class='): link_class = arg.split('=')[1] elif arg in ('skip_enlarge', 'no_enlarge'): enlarge = False elif arg.startswith('class='): img_css = arg.split('=')[1] elif arg.startswith('alt='): alt = arg.split('=')[1] elif arg.startswith('width='): responsive = False try: width = int(arg.split('=')[1]) except ValueError: width = 0 elif arg.startswith('height='): responsive = False try: height = int(arg.split('=')[1]) except ValueError: height = 0 if images_width: responsive = False width = images_width # HTML code if responsive: r = img.get_responsive_html(alt, enlarge=enlarge, css=util.escape_html(img_css)) else: r = img.get_html(alt, width=width, height=height, enlarge=enlarge, css=util.escape_html(img_css)) # Link to original file if link_orig: link = htmler.A(r, href=img.url, target=link_target, title=util.escape_html(alt)) if link_class: link.set_attr('css', util.escape_html(link_class)) r = str(link) return r