VACUMM / sphinx-fortran

Fortran domain and autodoc extensions to Sphinx
Other
45 stars 29 forks source link

Let the comment header as raw text or literal block #45

Open ndarmage opened 2 years ago

ndarmage commented 2 years ago

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.

quocdang1998 commented 2 years ago

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  *  *  *
!*