예제 #1
0
    def render_svg_image(self, node):
        from abjad.tools import abjadbooktools
        # Get all file and path parts.
        image_layout_specifier = node.get('image_layout_specifier', None)
        if image_layout_specifier is None:
            image_layout_specifier = abjadbooktools.ImageLayoutSpecifier()
        image_render_specifier = node.get('image_render_specifier', None)
        if image_render_specifier is None:
            image_render_specifier = abjadbooktools.ImageRenderSpecifier()
        pages = image_layout_specifier.pages

        target_extension = '.svg'
        file_base_name = SphinxDocumentHandler.get_file_base_name(
            node, image_render_specifier)
        source_extension = SphinxDocumentHandler.get_source_extension(node)
        file_name_pattern = '{}*{}'.format(file_base_name, target_extension)
        absolute_directory_path, relative_directory_path = \
            SphinxDocumentHandler.get_image_directory_paths(self)
        relative_source_file_path = posixpath.join(
            relative_directory_path,
            file_base_name + source_extension,
        )

        # Check for pre-existing target(s).
        target_file_paths, found_all_pages = \
            SphinxDocumentHandler.find_target_file_paths(
                absolute_directory_path, relative_directory_path,
                file_name_pattern, pages)
        if found_all_pages:
            return (relative_source_file_path, target_file_paths)

        # Write and render source to target(s).
        source_file_name = file_base_name + source_extension
        absolute_source_file_path = os.path.join(absolute_directory_path,
                                                 source_file_name)
        target_file_name = file_base_name + target_extension
        absolute_target_file_path = os.path.join(absolute_directory_path,
                                                 target_file_name)
        SphinxDocumentHandler.write_image_source(self, node,
                                                 absolute_source_file_path)
        return_code = SphinxDocumentHandler.interpret_image_source(
            self,
            node,
            absolute_source_file_path,
            absolute_target_file_path,
            file_format='svg')
        if return_code:
            return (relative_directory_path, [])

        # Check for target(s).
        target_file_paths, found_all_pages = \
            SphinxDocumentHandler.find_target_file_paths(
                absolute_directory_path, relative_directory_path,
                file_name_pattern, pages)
        if not found_all_pages:
            return (relative_source_file_path, target_file_paths)

        # Target(s) must exist, so simply return.
        return (relative_source_file_path, target_file_paths)
예제 #2
0
 def visit_abjad_output_block_html(self, node):
     from abjad.tools import abjadbooktools
     image_layout_specifier = node.get('image_layout_specifier', None)
     if image_layout_specifier is None:
         image_layout_specifier = abjadbooktools.ImageLayoutSpecifier()
     image_render_specifier = node.get('image_render_specifier', None)
     if image_render_specifier is None:
         image_render_specifier = abjadbooktools.ImageRenderSpecifier()
     if node['renderer'] not in ('graphviz', 'lilypond'):
         raise nodes.SkipNode
     absolute_image_directory_path = os.path.join(
         self.builder.outdir,
         self.builder.imagedir,
         'abjadbook',
     )
     if not os.path.exists(absolute_image_directory_path):
         os.makedirs(absolute_image_directory_path)
     try:
         if node['renderer'] == 'graphviz':
             relative_source_file_path, relative_target_file_paths = \
                 SphinxDocumentHandler.render_svg_image(self, node)
         else:
             relative_source_file_path, relative_target_file_paths = \
                 SphinxDocumentHandler.render_png_image(self, node)
         if image_layout_specifier.with_thumbnail:
             for relative_target_file_path in relative_target_file_paths:
                 self.builder.thumbnails.add_file(
                     '', relative_target_file_path)
         css_classes = set(node.get('cls', '').split())
         template = SphinxDocumentHandler._basic_template
         if image_layout_specifier.with_thumbnail:
             css_classes.add('thumbnail')
             template = SphinxDocumentHandler._thumbnail_template
         if image_layout_specifier.with_columns:
             css_classes.add('table-cell')
             stop = len(relative_target_file_paths)
             step = image_layout_specifier.with_columns
             for i in range(0, stop, step):
                 self.body.append(
                     SphinxDocumentHandler._table_row_open_template)
                 paths = relative_target_file_paths[i:i + step]
                 for relative_target_file_path in paths:
                     prefix, suffix = os.path.splitext(
                         relative_target_file_path)
                     if suffix == '.svg':
                         thumbnail_path = relative_target_file_path
                     else:
                         thumbnail_path = '{}-thumbnail{}'.format(
                             prefix, suffix)
                     result = template.format(
                         alt='',
                         cls=' '.join(sorted(css_classes)),
                         group='group-{}'.format(
                             os.path.basename(relative_source_file_path)),
                         source_path=relative_source_file_path,
                         target_path=relative_target_file_path,
                         title='',
                         thumbnail_path=thumbnail_path,
                     )
                     result = stringtools.normalize(result)
                     result = ('    ' + _ for _ in result.splitlines())
                     result = '\n'.join(result)
                     self.body.append(result)
                 self.body.append(
                     SphinxDocumentHandler._table_row_close_template)
         else:
             css_classes.add('abjadbook')
             for relative_target_file_path in relative_target_file_paths:
                 prefix, suffix = os.path.splitext(
                     relative_target_file_path)
                 if suffix == '.svg':
                     thumbnail_path = relative_target_file_path
                 else:
                     thumbnail_path = '{}-thumbnail{}'.format(
                         prefix, suffix)
                 result = template.format(
                     alt='',
                     cls=' '.join(sorted(css_classes)),
                     group='group-{}'.format(
                         os.path.basename(relative_source_file_path)),
                     source_path=relative_source_file_path,
                     target_path=relative_target_file_path,
                     title='',
                     thumbnail_path=thumbnail_path,
                 )
                 result = stringtools.normalize(result)
                 self.body.append(result)
     except:
         traceback.print_exc()
     raise nodes.SkipNode