sphinx-fortran icon indicating copy to clipboard operation
sphinx-fortran copied to clipboard

Let the comment header as raw text or literal block

Open ndarmage opened this issue 2 years ago • 1 comments

Hello,

Is it possible to leave the comment header of fortran blocks as raw text without interpreting it as ReST please ? A possible fix is adding the following lines,

if len(block['desc']) and raw_desc > 0:
    block['desc'] = ["! .. code-block::\n"] + block['desc']

right after,

# Comment
 block['desc'] = self.get_comment(subsrc, aslist=True)

in the function scan_container of file fortran_autodoc.f90. The new variable raw_desc should be True if such option was selected when processing the fortran blocks.

Or perhaps, is there another way of achiving this behavior?

Thanks in advance for your kind attention.

ndarmage avatar Jun 22 '22 14:06 ndarmage

In the definition of F90toRst.scan_container, you can add these following lines:

        # Comment
        block['desc'] = self.get_comment(subsrc, aslist=True)
        indent = ''
        in_codeblock = False
        i = 0
        while i < len(block['desc']):
            line = block['desc'][i]
            if not in_codeblock:
                if line.startswith(' '):   # get current indent
                    indent = re.split(r'[^\s]', line)[0]
                if line == '*':  # code-block starts with !*
                    in_codeblock = True
                    block['desc'][i] = indent + '.. code-block::'
                    block['desc'].insert(i, '')  # insert empty line before directive
                    i += 2
                    block['desc'].insert(i, '')  # insert empty line after directive
            else:
                if line == '*':  # code-block ends with !*
                    in_codeblock = False
                    block['desc'][i] = ''
                    continue
                block['desc'][i] = indent + '   ' + line
            i += 1

Please note that a code-block directive will start and end with !*. Here's a MWE:

!*
!      11 22 33 44 55 66 77 88 99
!      21 32 43 54 65 76 87 98  *
!      31 42 53 64 75 86 97  *  *
!      41 52 63 74 85 96  *  *  *
!*

quocdang1998 avatar Jun 22 '22 15:06 quocdang1998