aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/kernel-doc
diff options
context:
space:
mode:
authorJonathan Corbet <corbet@lwn.net>2018-02-06 17:58:45 -0500
committerJonathan Corbet <corbet@lwn.net>2018-02-15 15:11:27 -0500
commitaf250290430e1e678eef8c5c646a1bfd6af7b68a (patch)
treeddae2c4401af821831404688bde061f6803d2e22 /scripts/kernel-doc
parentc17add56ca4ee618b07ed9a21e2a29f0a90dc0ba (diff)
docs: kernel-doc: Don't mangle literal code blocks in comments
It can be useful to put code snippets into kerneldoc comments; that can be done with the "::" operator at the end of a line like this:: if (desperate) run_in_circles(); The ".. code-block::" directive can also be used to this end. kernel-doc currently fails to understand these literal blocks and applies its normal markup to them, which is then treated as literal by sphinx. The result is unsightly markup instead of a useful code snippet. Apply a hack to the output code to recognize literal blocks and avoid performing any special markup on them. It's ugly, but that means it fits in well with the rest of the script. Reviewed-by: Jani Nikula <jani.nikula@intel.com> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'scripts/kernel-doc')
-rwxr-xr-xscripts/kernel-doc69
1 files changed, 64 insertions, 5 deletions
diff --git a/scripts/kernel-doc b/scripts/kernel-doc
index fb8fbdb25036..cbe864e72a2f 100755
--- a/scripts/kernel-doc
+++ b/scripts/kernel-doc
@@ -748,14 +748,73 @@ sub output_blockhead_rst(%) {
748 } 748 }
749} 749}
750 750
751sub output_highlight_rst { 751#
752 my $contents = join "\n",@_; 752# Apply the RST highlights to a sub-block of text.
753 my $line; 753#
754 754sub highlight_block($) {
755 # The dohighlight kludge requires the text be called $contents
756 my $contents = shift;
755 eval $dohighlight; 757 eval $dohighlight;
756 die $@ if $@; 758 die $@ if $@;
759 return $contents;
760}
757 761
758 foreach $line (split "\n", $contents) { 762#
763# Regexes used only here.
764#
765my $sphinx_literal = '^[^.].*::$';
766my $sphinx_cblock = '^\.\.\ +code-block::';
767
768sub output_highlight_rst {
769 my $input = join "\n",@_;
770 my $output = "";
771 my $line;
772 my $in_literal = 0;
773 my $litprefix;
774 my $block = "";
775
776 foreach $line (split "\n",$input) {
777 #
778 # If we're in a literal block, see if we should drop out
779 # of it. Otherwise pass the line straight through unmunged.
780 #
781 if ($in_literal) {
782 if (! ($line =~ /^\s*$/)) {
783 #
784 # If this is the first non-blank line in a literal
785 # block we need to figure out what the proper indent is.
786 #
787 if ($litprefix eq "") {
788 $line =~ /^(\s*)/;
789 $litprefix = '^' . $1;
790 $output .= $line . "\n";
791 } elsif (! ($line =~ /$litprefix/)) {
792 $in_literal = 0;
793 } else {
794 $output .= $line . "\n";
795 }
796 } else {
797 $output .= $line . "\n";
798 }
799 }
800 #
801 # Not in a literal block (or just dropped out)
802 #
803 if (! $in_literal) {
804 $block .= $line . "\n";
805 if (($line =~ /$sphinx_literal/) || ($line =~ /$sphinx_cblock/)) {
806 $in_literal = 1;
807 $litprefix = "";
808 $output .= highlight_block($block);
809 $block = ""
810 }
811 }
812 }
813
814 if ($block) {
815 $output .= highlight_block($block);
816 }
817 foreach $line (split "\n", $output) {
759 print $lineprefix . $line . "\n"; 818 print $lineprefix . $line . "\n";
760 } 819 }
761} 820}