wikiheaders.pl: Properly handle and wordwrap bullet lists.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/build-scripts/wikiheaders.pl b/build-scripts/wikiheaders.pl
index e25ef11..ead91a8 100755
--- a/build-scripts/wikiheaders.pl
+++ b/build-scripts/wikiheaders.pl
@@ -17,6 +17,81 @@ foreach (@ARGV) {
$wikipath = $_, next if not defined $wikipath;
}
+my $wordwrap_mode = 'mediawiki';
+sub wordwrap_atom { # don't call this directly.
+ my $str = shift;
+ return fill('', '', $str);
+}
+
+sub wordwrap_with_bullet_indent { # don't call this directly.
+ my $bullet = shift;
+ my $str = shift;
+ my $retval = '';
+
+ # You _can't_ (at least with Pandoc) have a bullet item with a newline in
+ # MediaWiki, so _remove_ wrapping!
+ if ($wordwrap_mode eq 'mediawiki') {
+ $retval = "$bullet$str";
+ $retval =~ s/\n/ /gms;
+ return "$retval\n";
+ }
+
+ my $bulletlen = length($bullet);
+
+ # wrap it and then indent each line to be under the bullet.
+ $Text::Wrap::columns -= $bulletlen;
+ my @wrappedlines = split /\n/, wordwrap_atom($str);
+ $Text::Wrap::columns += $bulletlen;
+
+ my $prefix = $bullet;
+ my $usual_prefix = ' ' x $bulletlen;
+
+ foreach (@wrappedlines) {
+ $retval .= "$prefix$_\n";
+ $prefix = $usual_prefix;
+ }
+
+ return $retval;
+}
+
+sub wordwrap_one_paragraph { # don't call this directly.
+ my $retval = '';
+ my $p = shift;
+ #print "\n\n\nPARAGRAPH: [$p]\n\n\n";
+ if ($p =~ s/\A([\*\-] )//) { # bullet list, starts with "* " or "- ".
+ my $bullet = $1;
+ my $item = '';
+ my @items = split /\n/, $p;
+ foreach (@items) {
+ if (s/\A([\*\-] )//) {
+ $retval .= wordwrap_with_bullet_indent($bullet, $item);
+ $item = '';
+ }
+ s/\A\s*//;
+ $item .= "$_\n"; # accumulate lines until we hit the end or another bullet.
+ }
+ if ($item ne '') {
+ $retval .= wordwrap_with_bullet_indent($bullet, $item);
+ }
+ } else {
+ $retval = wordwrap_atom($p) . "\n";
+ }
+
+ return $retval;
+}
+
+sub wordwrap_paragraphs { # don't call this directly.
+ my $str = shift;
+ my $retval = '';
+ my @paragraphs = split /\n\n/, $str;
+ foreach (@paragraphs) {
+ next if $_ eq '';
+ $retval .= wordwrap_one_paragraph($_);
+ $retval .= "\n";
+ }
+ return $retval;
+}
+
my $wordwrap_default_columns = 76;
sub wordwrap {
my $str = shift;
@@ -28,26 +103,15 @@ sub wordwrap {
my $retval = '';
- # !!! FIXME: at some point it would be neat if this understood lists, so
- #
- # * Item 1
- # * Item 2
- #
- # was understood to a) not wrap into one line and b) knew to wrap so overflow
- # lined up _indented_ under the '*' (etc) char. But we don't do that currently,
- # so cheat and make each bullet its own paragraph instead, which is Good Enough
- # for the time being.
-
- while ($str =~ s/(.*?)(\n*\`\`\`.*?\`\`\`\n*|\n*\<syntaxhighlight.*?\<\/syntaxhighlight\>\n*)//ms) {
- $retval .= fill('', '', $1); # wrap it.
+ while ($str =~ s/(.*?)(\n+\`\`\`.*?\`\`\`\n+|\n+\<syntaxhighlight.*?\<\/syntaxhighlight\>\n+)//ms) {
+ $retval .= wordwrap_paragraphs($1); # wrap it.
$retval .= $2; # don't wrap it.
}
- return $retval . fill('', '', $str); # wrap what's left.
+ return $retval . wordwrap_paragraphs($str); # wrap what's left.
}
-
sub wikify {
my $wikitype = shift;
my $str = shift;
@@ -70,6 +134,9 @@ sub wikify {
# italic
$str =~ s/\*(.*?)\*/''$1''/gms;
+
+ # bullets
+ $str =~ s/^\- /* /gm;
} elsif ($wikitype eq 'md') {
# Convert obvious SDL things to wikilinks.
$str =~ s/\b(SDL_[a-zA-Z0-9_]+)/[$1]($1)/gms;
@@ -146,6 +213,9 @@ sub dewikify {
# italic
$str =~ s/\''(.*?)''/*$1*/gms;
+
+ # bullets
+ $str =~ s/^\* /- /gm;
}
return $str;
@@ -529,9 +599,13 @@ if ($copy_direction == 1) { # --copy-to-headers
} elsif ($copy_direction == -1) { # --copy-to-wiki
foreach (keys %headerfuncs) {
my $fn = $_;
- my $wikitype = defined $wikitypes{$fn} ? $wikitypes{$fn} : 'md'; # default to Markdown for new stuff.
+ my $wikitype = defined $wikitypes{$fn} ? $wikitypes{$fn} : 'mediawiki'; # default to MediaWiki for new stuff FOR NOW.
die("Unexpected wikitype '$wikitype'\n") if (($wikitype ne 'mediawiki') and ($wikitype ne 'md'));
+ #print("$fn\n"); next;
+
+ $wordwrap_mode = $wikitype;
+
my $raw = $headerfuncs{$fn}; # raw doxygen text with comment characters stripped from start/end and start of each line.
$raw =~ s/\A\s*\\brief\s+//; # Technically we don't need \brief (please turn on JAVADOC_AUTOBRIEF if you use Doxygen), so just in case one is present, strip it.
@@ -549,7 +623,7 @@ if ($copy_direction == 1) { # --copy-to-headers
$brief =~ s/\A(.*?\.) /$1\n\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
my @briefsplit = split /\n/, $brief;
- $brief = wikify($wikitype, shift @briefsplit);
+ $brief = wikify($wikitype, shift @briefsplit) . "\n";
@doxygenlines = (@briefsplit, @doxygenlines);
my $remarks = '';
@@ -634,13 +708,16 @@ if ($copy_direction == 1) { # --copy-to-headers
}
}
+ # Make sure this ends with a double-newline.
+ $sections{'Related Functions'} .= "\n" if defined $sections{'Related Functions'};
+
# We can build the wiki table now that we have all the data.
if (scalar(@params) > 0) {
my $str = '';
if ($wikitype eq 'mediawiki') {
while (scalar(@params) > 0) {
my $arg = shift @params;
- my $desc = shift @params;
+ my $desc = wikify($wikitype, shift @params);
$str .= ($str eq '') ? "{|\n" : "|-\n";
$str .= "|'''$arg'''\n";
$str .= "|$desc\n";
@@ -651,11 +728,12 @@ if ($copy_direction == 1) { # --copy-to-headers
my $longest_desc = 0;
my $which = 0;
foreach (@params) {
- my $len = length($_);
if ($which == 0) {
+ my $len = length($_) + 4;
$longest_arg = $len if ($len > $longest_arg);
$which = 1;
} else {
+ my $len = length(wikify($wikitype, $_));
$longest_desc = $len if ($len > $longest_desc);
$which = 0;
}
@@ -667,7 +745,7 @@ if ($copy_direction == 1) { # --copy-to-headers
while (@params) {
my $arg = shift @params;
- my $desc = shift @params;
+ my $desc = wikify($wikitype, shift @params);
$str .= "| **$arg** " . (' ' x ($longest_arg - length($arg))) . "| $desc" . (' ' x ($longest_desc - length($desc))) . " |\n";
}
} else {