Example #1
0
    def _process_diagram(self, options: Options, body: str) -> str:
        '''
        Add PlantUML diagram to execution queue if it was not found in cache. Save the diagram
        source into .diag file for debug.

        Finally, replace diagram definition with a buffer tag. After the queue is processed
        this tag will be replaced by Markdown image or inline diagram code.

        :param options: Options extracted from the diagram definition
        :param body: PlantUML diagram body

        :returns: Buffer tag to be processed on the second pass.
        '''

        diagram_source = parse_diagram_source(body)
        if not diagram_source:
            self._warning(
                'Cannot parse diagram body. Have you forgotten @startuml or @enduml?'
            )
            return ''

        self._cache_path.mkdir(parents=True, exist_ok=True)

        self.logger.debug(
            f'Processing PlantUML diagram, options: {options}, body: {body}')

        diag_format = get_diagram_format(options)
        original_params = options.get('params', {})
        if not isinstance(original_params, dict):
            raise ValueError(
                f'"params" should be dict, got {type(original_params).__name__}'
            )
        cmd_args = generate_args(original_params, diag_format,
                                 options['plantuml_path'])

        body_hash = md5(f'{cmd_args}{body}'.encode())
        diag_output_path = self._cache_path / f'{body_hash.hexdigest()}.{diag_format}'

        if diag_output_path.exists():
            self.logger.debug('Diagram image found in cache')
        else:
            self.logger.debug('Adding diagram to queue')
            self._add_to_queue(cmd_args, diagram_source, diag_output_path)
            self.logger.debug('Diagram added to queue')

        # saving diagram source into file for debug
        diag_src_path = diag_output_path.with_suffix('.diag')
        with open(diag_src_path, 'w', encoding='utf8') as diag_src_file:
            diag_src_file.write(body)

        buffer_tag = generate_buffer_tag(diag_output_path, options)
        self.logger.debug(f'Generated buffer tag: {buffer_tag}')
        return buffer_tag
Example #2
0
def generate_buffer_tag(diagram_path: PosixPath, options: Options) -> str:
    '''
    Generate a buffer tag which should be put in place of a PlantUML diagram in Markdown source.

    After processing the queue these tags should be replaced by Markdown image tags or inline
    diagram code.

    :param diagram_path: path to the generated diagram image file (if not in cache, it doesn't exist
                         at this stage).
    :param options: diagram options.

    :returns string with a generated buffer tag:
    '''
    allow_inline = ('.svg', )

    inline = diagram_path.suffix in allow_inline and options[
        'as_image'] is False
    caption = options.get('caption', '')

    result = f'<{BUFFER_TAG} file="{diagram_path}" inline="{inline}" caption="{caption}"></{BUFFER_TAG}>'

    return result