Skip to content

Commit

Permalink
doc/sphinx: Track line-number of starting blocks
Browse files Browse the repository at this point in the history
Design is pretty simple: kernel-doc inserts breadcrumbs with line
numbers, and sphinx picks them up. At first I went with a sphinx
comment, but inserting those at random places seriously upsets the
parser, and must be filtered. Hence why this version now uses "#define
LINEO " since one of these ever escape into output it's pretty clear
there is a bug.

It seems to work well, and at least the 2-3 errors where sphinx
complained about something that was not correct in kernel-doc text the
line numbers matched up perfectly.

v2: Instead of noodling around in the parser state machine, create
a ViewList and parse it ourselves. This seems to be the recommended
way, per Jani's suggestion.

v3:
- Split out ViewList pach. Splitting the kernel-doc changes from the
  sphinx ones isn't possible, since emitting the LINENO lines wreaks
  havoc with the rst formatting. We must filter them.

- Improve the regex per Jani's suggestions, and compile it just once
  for speed.

- Now that LINENO lines are eaten, also add them to function parameter
  descriptions. Much less content and offset than for in-line struct
  member descriptions, but still nice to know which exact continuation
  line upsets sphinx.

- Simplify/clarify the line +/-1 business a bit.

v4: Split out the scripts/kernel-doc changes and make line-numbers
opt-in, as suggested by Jani.

Cc: Jani Nikula <jani.nikula@intel.com>
Cc: linux-doc@vger.kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
  • Loading branch information
Daniel Vetter authored and Jani Nikula committed Jun 4, 2016
1 parent 0b0f5f2 commit d90368f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Documentation/sphinx/kernel-doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import os
import subprocess
import sys
import re

from docutils import nodes, statemachine
from docutils.statemachine import ViewList
Expand All @@ -50,7 +51,7 @@ class KernelDocDirective(Directive):

def run(self):
env = self.state.document.settings.env
cmd = [env.config.kerneldoc_bin, '-rst']
cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno']

filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]

Expand Down Expand Up @@ -93,7 +94,19 @@ def run(self):
sys.stderr.write(err)

lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
result = ViewList(lines, source)
result = ViewList()

lineoffset = 0;
line_regex = re.compile("^#define LINENO ([0-9]+)$")
for line in lines:
match = line_regex.search(line)
if match:
# sphinx counts lines from 0
lineoffset = int(match.group(1)) - 1
# we must eat our comments since the upset the markup
else:
result.append(line, source, lineoffset)
lineoffset += 1

node = nodes.section()
node.document = self.state.document
Expand Down

0 comments on commit d90368f

Please sign in to comment.