edit-paragraphs.pl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # Copyright (C) 2014 Alex Schroeder <alex@gnu.org>
  2. # This program is free software: you can redistribute it and/or modify it under
  3. # the terms of the GNU General Public License as published by the Free Software
  4. # Foundation, either version 3 of the License, or (at your option) any later
  5. # version.
  6. #
  7. # This program is distributed in the hope that it will be useful, but WITHOUT
  8. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License along with
  12. # this program. If not, see <http://www.gnu.org/licenses/>.
  13. use strict;
  14. use v5.10;
  15. AddModuleDescription('edit-paragraphs.pl', 'Edit Paragraphs Extension');
  16. our ($q, $OpenPageName, $Fragment, %Page, %Action, @MyRules, $LastUpdate);
  17. # edit icon
  18. # http://publicdomainvectors.org/en/free-clipart/Pencil-vector-icon/9221.html
  19. # q{<img width="30" height="30" title="" alt="edit" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAE2UlEQVRIx53WW0sbaRwG8GcmSsxhdTKT5K60FFqa4CmKh96uq62gtR563butN1KNQSgESqFemBjr2mKjMRpBt1uln2FJe9ObHsyu3d2yyH6EFoTVhr7PXrQTJslMmuwLA0mYvL95/vOebPjSJJMLJZ+t7iu9pw9AHEAQwAGAE4v/Wnasf5dNHsQK/R7A37IsEwAB/ATAY9W/VUpjh9Wi7wHw5s2b4vz588KAKyb9fDMVqniwPgB/AWA0GhX5fF68fftWBAIBHV82wf83bET/AMDJyUnx4cMHwa/t1atXRry07AW0FlhvPwA4dDgclCRJtLe3ixc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thmt2tFbBmuaRiMshODJyQlfvnzJoaEhAuDAwAD39vZEKBQyLg6eGk6h5XB9fT0zmUwRTpKnp6d8/Pgx+/v7+ezZM+PWlrI4sko1lfrre+bm5mYZ/vnzZ2azWREMBnU0XbLLSFVureawjm9tbRUdV3K5nAgEAgQgAGQAuGpALRNLBlgCAKfTib6+PtjtdpyenvLw8FA6OjoSAH4G8COAfyuVzyKYafvVmNrkygPYAWCvMWnFUv8Hwmcq2TCQ4MwAAAAASUVORK5CYII=" />};
  20. our $EditParagraphPencil = '&#x270E;';
  21. # Allow editing of substrings
  22. $Action{'edit-paragraph'} = \&DoEditParagraph;
  23. sub DoEditParagraph {
  24. my $id = UnquoteHtml(GetParam('title', ''));
  25. UserCanEditOrDie($id);
  26. my $old = UnquoteHtml(GetParam('paragraph', ''));
  27. $old =~ s/\r//g;
  28. return DoEdit($id) unless $old;
  29. my $around = GetParam('around', undef);
  30. # Find text to edit
  31. my $new = GetParam('text', '');
  32. OpenPage($id);
  33. if ($new) {
  34. my $myoldtime = GetParam('oldtime', ''); # maybe empty!
  35. my $text;
  36. if ($myoldtime and $myoldtime != $LastUpdate) {
  37. ($text) = GetTextAtTime($myoldtime);
  38. } else {
  39. $text = $Page{text};
  40. }
  41. my $done;
  42. if ($around) {
  43. # The tricky part is that the numbers refer to the HTML quoted text. What a pain.
  44. my $qold = QuoteHtml($old);
  45. my $qtext = QuoteHtml($text);
  46. if (substr($qtext, $around - length($qold), length($qold)) eq $qold) {
  47. $text = UnquoteHtml(substr($qtext, 0, $around - length($qold))
  48. . QuoteHtml($new) . substr($qtext, $around));
  49. $done = 1;
  50. }
  51. } else {
  52. # simple case, just do it
  53. my $search_term = quotemeta($old);
  54. $done = $text =~ s/$search_term/$new/;
  55. }
  56. if ($done) {
  57. SetParam('text', UnquoteHtml($text));
  58. return DoPost($id);
  59. } else {
  60. $text = substr($text, 0, $around)
  61. . "\n### around here ###\n"
  62. . substr($text, $around)
  63. if $around;
  64. ReportError(T('Could not identify the paragraph you were editing'),
  65. '500 INTERNAL SERVER ERROR',
  66. undef,
  67. $q->p(T('This is the section you edited:'))
  68. . $q->pre(QuoteHtml($old))
  69. . $q->p(T('This is the current page:'))
  70. . $q->pre($text));
  71. }
  72. }
  73. print GetHeader('', Ts('Editing %s', NormalToFree($id)));
  74. print $q->start_div({-class=>'content edit paragraph'});
  75. my $form = GetEditForm($id, undef, $old);
  76. my $param = GetHiddenValue('paragraph', $old);
  77. $param .= GetHiddenValue('action', 'edit-paragraph'); # add action
  78. $param .= GetHiddenValue('around', $around); # add around position
  79. $form =~ s!</form>!$param</form>!;
  80. print $form;
  81. print $q->end_div();
  82. PrintFooter($id, 'edit');
  83. }
  84. # When PrintWikiToHTML is called for the current revision of a page we
  85. # initialize our data structure. The data structure simply divides the
  86. # page up into blocks based on what one would like to edit. By
  87. # default, that's just paragraph breaks and list items. When using
  88. # Creole, ordered list items and table rows are added.
  89. my @EditParagraphs = ();
  90. *EditParagraphOldPrintWikiToHTML = \&PrintWikiToHTML;
  91. *PrintWikiToHTML = \&EditParagraphNewPrintWikiToHTML;
  92. sub EditParagraphNewPrintWikiToHTML {
  93. my ($text, $is_saving_cache, $revision, $is_locked) = @_;
  94. # We need to use quoted HTML because that's what the rules will applied to!
  95. my $quoted_text = QuoteHtml($text);
  96. if ($quoted_text and not $revision) {
  97. my ($start, $end) = (0, 0);
  98. # This grouping with zero-width positive look-ahead assertion makes sure that this chunk of text does not include
  99. # markup need for the next chunk of text.
  100. my $regexp;
  101. if (grep { $_ eq \&CreoleRule } @MyRules) {
  102. $regexp = "\n+(\n|(?=[*#-=|]))";
  103. } else {
  104. $regexp = "\n+(\n|(?=[*]))";
  105. }
  106. while ($quoted_text =~ /$regexp/g) {
  107. $end = pos($quoted_text);
  108. push(@EditParagraphs,
  109. [$start, $end, substr($quoted_text, $start, $end - $start)]);
  110. $start = $end;
  111. }
  112. # Only do this if we have at least two paragraphs and the end isn't just some empty lines.
  113. if (@EditParagraphs and $start and $start < length($quoted_text)) {
  114. push(@EditParagraphs, [$start, length($quoted_text), substr($quoted_text, $start)]);
  115. }
  116. }
  117. # warn join('', '', map { $_->[0] . "-" . $_->[1] .": " . $_->[2]; } @EditParagraphs);
  118. return EditParagraphOldPrintWikiToHTML(@_);
  119. }
  120. # Whenever an important element is closed, we try to add a link.
  121. *EditParagraphOldCloseHtmlEnvironments = \&CloseHtmlEnvironments;
  122. *CloseHtmlEnvironments = \&EditParagraphNewCloseHtmlEnvironments;
  123. sub EditParagraphNewCloseHtmlEnvironments {
  124. EditParagraph();
  125. return EditParagraphOldCloseHtmlEnvironments(@_);
  126. }
  127. *EditParagraphOldCloseHtmlEnvironmentUntil = \&CloseHtmlEnvironmentUntil;
  128. *CloseHtmlEnvironmentUntil = \&EditParagraphNewCloseHtmlEnvironmentUntil;
  129. sub EditParagraphNewCloseHtmlEnvironmentUntil {
  130. my $tag = $_[0];
  131. if ($tag =~ /^(p|li|table|h[1-6])$/i) {
  132. EditParagraph();
  133. }
  134. return EditParagraphOldCloseHtmlEnvironmentUntil(@_);
  135. }
  136. sub EditParagraph {
  137. my $text;
  138. my $pos = pos; # pos is empty for the last link
  139. if (@EditParagraphs) {
  140. if ($pos) {
  141. while (@EditParagraphs and $EditParagraphs[0]->[1] <= $pos) {
  142. $pos = $EditParagraphs[0]->[1]; # just in case we're overshooting
  143. $text .= $EditParagraphs[0]->[2];
  144. shift(@EditParagraphs);
  145. }
  146. } else {
  147. # the last one
  148. $text = $EditParagraphs[-1]->[2];
  149. }
  150. }
  151. if ($text) {
  152. # Huge Hack Alert: We are appending to $Fragment, which is what Clean appends to. We do this so that we can handle
  153. # headers and other block elements. Without this fix we'd see something like this:
  154. # <h2>...</h2><p><a ...>&#x270E;</a></p>
  155. # Usually this would look as follows:
  156. # <h2>...</h2><p></p>
  157. # This is eliminated in Dirty. But it won't be eliminated if we leave the link in there. What we want is this:
  158. # <h2>...<a ...>&#x270E;</a></h2><p></p>
  159. #
  160. # The same issue arises for other block level elements. What happens at the end of a table? Without this fix we'd
  161. # see something like this:
  162. # <table><tr><td>...</td></tr></table><p><a ...>&#x270E;</a></p>
  163. # What we want, I guess, is this:
  164. # <table><tr><td>...<a ...>&#x270E;</a></td></tr></table></p>
  165. $pos = $pos || length(QuoteHtml($Page{text})); # make sure we have an around value
  166. my $title = UrlEncode($OpenPageName);
  167. my $paragraph = UrlEncode(UnquoteHtml($text));
  168. my $link = ScriptLink("action=edit-paragraph;title=$title;around=$pos;paragraph=$paragraph",
  169. $EditParagraphPencil, 'pencil');
  170. if ($Fragment =~ s!((:?</h[1-6]>|</t[dh]></tr></table>|</pre>)<p>)$!!) {
  171. # $Fragment .= '<!-- moved inside -->';
  172. $Fragment .= $link . $1;
  173. } elsif ($Fragment =~ s!(</p>\s*</form>)$!!) {
  174. # $Fragment .= '<!-- HTML fixes for <html> -->';
  175. # Since anything can appear in raw HTML tags, there is no one-size fits all rule.
  176. # I usually use the <html> tags to embed forms, and forms need to contain a <p>.
  177. # so that's what I'm handling.
  178. $Fragment .= $link . $1;
  179. } elsif ($pos and $Fragment =~ /<(p|tr)>$/) {
  180. # Do nothing: this is either an empty paragraph and will be
  181. # eliminated, or an empty row which will not be shown.
  182. # $Fragment .= '<!-- empty -->';
  183. } else {
  184. # This is the default: add the link.
  185. # $Fragment .= '<!-- default -->';
  186. $Fragment .= $link;
  187. }
  188. }
  189. }