Esempio n. 1
0
 def visit_image(self, img_node):
     if self.__local_mode:
         if not isdir(self.temp_dir):
             self.temp_dir = mkdtemp(prefix='img_')
         img_url = img_node.attributes['uri']
         if isfile(img_url):
             return
         if not is_url(img_url):
             raise ImageURLError('Invalid image URL: "%s"' % (img_url,))
         elif img_url in self.localized_images:
             local_filename = self.localized_images[img_url]
             if not isfile(temp_local_filename):
                 self.localize_image(img_url, local_filename=local_filename)
             img_node.attributes['uri'] = local_filename
             return
         else:
             self.localized_images[img_url] = self.localize_image
     else:
         img_path = img_node.attributes['uri']
         if not isfile(img_path) and not is_url(img_path):
             raise ImageURLError('Invalid image URL: "%s"' % (img_path,))
         if is_url(img_path):
             return
         elif isfile(img_path):
             try:
                 img_url = reverse_lookup(self.localized_images, img_path)
             except ValueError:
                 return
             else:
                 img_node.attributes['uri'] = img_url
                 os.remove(img_path)
Esempio n. 2
0
def doctree_to_html(doctree, stylesheet_url='',
    settings=DEFAULT_HTML_OVERRIDES, tidy_output=True,
    tidy_settings=DEFAULT_TIDY_HTML_OPTIONS, *args, **kwargs):
    conversion_settings = copy(settings)
    if tidy is None:
        tidy_output = False
    if is_url(stylesheet_url, net_loc=('http', 'ftp')):
        conversion_settings['stylesheet-path'] = stylesheet_url
    html_string = publish_from_doctree(doctree, writer_name='html4css1',
        settings_overrides=conversion_settings, *args, **kwargs)
    if tidy_output:
        html_string = str(tidy.parseString(html_string, **tidy_settings))
    return html_string