123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- use strict;
- use v5.10;
- AddModuleDescription('creole.pl', 'Creole Markup Extension');
- our ($q, $bol, %InterSite, $FullUrlPattern, $FreeLinkPattern, $FreeInterLinkPattern, $InterSitePattern, @MyRules, %RuleOrder, @MyInitVariables, @HtmlStack, @HtmlAttrStack);
- our ($CreoleLineBreaks,
- $CreoleTildeAlternative,
- $CreoleTableCellsContainBlockLevelElements,
- $CreoleDashStyleUnorderedLists);
- $CreoleLineBreaks = 0;
- $CreoleTildeAlternative = 0;
- $CreoleTableCellsContainBlockLevelElements = 0;
- $CreoleDashStyleUnorderedLists = 0;
- push(@MyInitVariables, \&CreoleInit);
- my $CreoleIsCreoleAddition;
- my $CreoleIsTableCellBol;
- my $CreoleTableCellPattern = '[ \t]*(\|+)(=)?\n?([ \t]*)';
- my $CreoleLinkPipePattern = '[ \t]*\|[ \t]*';
- my $CreoleLinkTextPattern = "($CreoleLinkPipePattern(.+?))?";
- my ($CreoleHeaderHtmlTag, $CreoleHeaderHtmlTagAttr);
- sub CreoleInit {
- $CreoleIsCreoleAddition = defined &CreoleAdditionRule;
- $CreoleIsTableCellBol =
- $CreoleHeaderHtmlTag =
- $CreoleHeaderHtmlTagAttr = '';
-
-
- if ($CreoleTableCellsContainBlockLevelElements) {
- SetHtmlEnvironmentContainer('td');
- SetHtmlEnvironmentContainer('th');
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- push(@MyRules,
- \&CreoleRule,
- \&CreoleHeadingRule,
- \&CreoleListAndNewLineRule);
- $RuleOrder{\&CreoleRule} = -10;
- $RuleOrder{\&CreoleHeadingRule} = 100;
- $RuleOrder{\&CreoleListAndNewLineRule} = 180;
- $RuleOrder{\&ListRule} = 190;
- sub CreoleRule {
-
-
-
-
- my ($is_interlinking, $is_intraanchoring) = @_;
-
-
- if ($bol && m/\G[ \t]*----[ \t]*(\n|$)/cg) {
- return CloseHtmlEnvironments().$q->hr().AddHtmlEnvironment('p');
- }
-
-
-
- elsif ($bol && m/\G\{\{\{[ \t]*\n(.*?)\n\}\}\}[ \t]*(\n|$)/cgs) {
- my $str = $1;
- return CloseHtmlEnvironments()
- .$q->pre({-class=> 'real'}, $str)
- .AddHtmlEnvironment('p');
- }
-
-
- elsif (m/\G(~($FullUrlPattern|\S))/cg) {
- return
- ($CreoleTildeAlternative and
- index( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- .'abcdefghijklmnopqrstuvwxyz'
- .'0123456789', $2) != -1)
- ? $1
- : $2;
- }
-
- elsif (m/\G\*\*/cg) { return AddOrCloseHtmlEnvironment('strong'); }
-
- elsif (m/\G\/\//cg) { return AddOrCloseHtmlEnvironment('em'); }
-
- elsif (m/\G\{\{\{(.*?}*)\}\}\}/cg) { return $q->code($1); }
-
- elsif (m/\G(\{\{$FreeLinkPattern$CreoleLinkTextPattern\}\})/cgs) {
- my $text = $4 || $2;
- return GetCreoleLinkHtml($1, GetDownloadLink(FreeToNormal($2), 1, undef, $text), $text);
- }
-
- elsif (m/\G\{\{$FullUrlPattern$CreoleLinkTextPattern\}\}/cgs) {
- return GetCreoleImageHtml(
- $q->a({-href=> UnquoteHtml($1),
- -class=> 'image outside'},
- $q->img({-src=> UnquoteHtml($1),
- -alt=> UnquoteHtml($3),
- -title=> UnquoteHtml($3),
- -class=> 'url outside',
- -loading=>'lazy'})));
- }
-
- elsif (m/\G(\[\[$FreeLinkPattern$CreoleLinkPipePattern
- \{\{$FreeLinkPattern$CreoleLinkTextPattern\}\}\]\])/cgsx) {
- my $text = $5 || $2;
- return GetCreoleLinkHtml($1, GetCreoleImageHtml(
- ScriptLink(UrlEncode(FreeToNormal($2)),
- $q->img({-src=> GetDownloadLink(FreeToNormal($3), 2),
- -alt=> UnquoteHtml($text),
- -title=> UnquoteHtml($text),
- -class=> 'upload',
- -loading=>'lazy'}), 'image')), $text);
- }
-
- elsif (m/\G(\[\[$FreeLinkPattern$CreoleLinkPipePattern
- \{\{$FullUrlPattern$CreoleLinkTextPattern\}\}\]\])/cgsx) {
- my $text = $5 || $2;
- return GetCreoleLinkHtml($1, GetCreoleImageHtml(
- ScriptLink(UrlEncode(FreeToNormal($2)),
- $q->img({-src=> UnquoteHtml($3),
- -alt=> UnquoteHtml($text),
- -title=> UnquoteHtml($text),
- -class=> 'url outside',
- -loading=>'lazy'}), 'image')), $text);
- }
-
- elsif (m/\G(\[\[$FullUrlPattern$CreoleLinkPipePattern
- \{\{$FreeLinkPattern$CreoleLinkTextPattern\}\}\]\])/cgsx) {
- my $text = $5 || $2;
- return GetCreoleLinkHtml($1, GetCreoleImageHtml(
- $q->a({-href=> UnquoteHtml($2), -class=> 'image outside'},
- $q->img({-src=> GetDownloadLink(FreeToNormal($3), 2),
- -alt=> UnquoteHtml($text),
- -title=> UnquoteHtml($text),
- -class=> 'upload',
- -loading=>'lazy'}))), $text);
- }
-
- elsif (m/\G\[\[$FullUrlPattern$CreoleLinkPipePattern
- \{\{$FullUrlPattern$CreoleLinkTextPattern\}\}\]\]/cgsx) {
- return GetCreoleImageHtml(
- $q->a({-href=> UnquoteHtml($1), -class=> 'image outside'},
- $q->img({-src=> UnquoteHtml($2),
- -alt=> UnquoteHtml($4),
- -title=> UnquoteHtml($4),
- -class=> 'url outside',
- -loading=>'lazy'})));
- }
-
- elsif (m/\G\[\[$FullUrlPattern$CreoleLinkTextPattern\]\]/cgs) {
-
-
- my $link_url = $1;
- my $link_text = $3 ? CreoleRuleRecursive($3, @_) : $link_url;
-
-
-
-
- return GetUrl($link_url, $link_text, 1);
- }
-
- elsif (m/\G(\[\[$FreeLinkPattern$CreoleLinkTextPattern\]\])/cgs) {
- my $markup = $1;
- my $page_name = $2;
- my $link_text = $4 ? CreoleRuleRecursive($4, @_) : $page_name;
- return GetCreoleLinkHtml($markup,
- GetPageOrEditLink($page_name, $link_text, 0, 1), $link_text);
- }
-
- elsif ($is_interlinking and
- m/\G(\[\[$FreeInterLinkPattern$CreoleLinkTextPattern\]\])/cgs) {
- my $markup = $1;
- my $interlink = $2;
- my $interlink_text = $4;
- my ($site_name, $page_name) = $interlink =~ m~^($InterSitePattern):(.*)$~;
-
-
-
- $interlink_text = $interlink_text
- ? CreoleRuleRecursive($interlink_text, @_)
- : $q->span({-class=> 'site'}, $site_name)
- .$q->span({-class=> 'separator'}, ':')
- .$q->span({-class=> 'page'}, $page_name);
-
-
-
- return GetCreoleLinkHtml($markup,
- $InterSite{$site_name}
- ? GetInterLink ($interlink, $interlink_text, 0, 1)
- : GetPageOrEditLink($page_name, $interlink_text, 0, 1), $interlink_text);
- }
-
-
-
-
-
- elsif (InElement('table')) {
-
-
-
-
-
-
-
-
-
-
- if (m/\G([ \t]*\|)?[ \t]*\n(?=$CreoleTableCellPattern)/cg) {
- return CloseHtmlEnvironmentUntil('table').AddHtmlEnvironment('tr');
- }
-
-
-
-
-
-
-
-
-
-
-
- elsif (m/\G[ \t]*\|[ \t]*(\n|$)/cg or
- (!$CreoleTableCellsContainBlockLevelElements and m/\G[ \t]*\n\n/cg)) {
-
-
-
-
-
-
-
-
- return CloseHtmlEnvironment('table').AddHtmlEnvironment('p');
- }
-
-
-
-
-
- elsif (m/\G$CreoleTableCellPattern/cg) {
-
-
-
-
-
-
- $CreoleIsTableCellBol = $CreoleTableCellsContainBlockLevelElements;
- my $tag = $2 ? 'th' : 'td';
- my $column_span = length($1);
- my $is_right_justified = $3;
-
- my $is_left_justified = m/\G(?=[^\n|]*?[ \t]+\|)/;
- my $attributes = $column_span == 1 ? '' : qq{colspan="$column_span"};
- if ($is_left_justified and
- $is_right_justified) { $attributes .= 'align="center"' }
- elsif ($is_right_justified) { $attributes .= 'align="right"' }
- elsif ($is_left_justified) { $attributes .= 'align="left"' }
- return
- (InElement('td') || InElement('th') ? CloseHtmlEnvironmentUntil('tr') : '')
- .AddHtmlEnvironment($tag, $attributes);
- }
- }
-
-
-
-
-
-
-
- elsif ($bol and m/\G(?=$CreoleTableCellPattern)/cg) {
- return OpenHtmlEnvironment('table', 1, 'user').AddHtmlEnvironment('tr');
- }
- return;
- }
- sub CreoleHeadingRule {
-
-
-
-
-
-
-
-
- if ($bol and m~\G(\s*\n)*(=+)[ \t]*~cg) {
- my $header_depth = length($2);
- ($CreoleHeaderHtmlTag, $CreoleHeaderHtmlTagAttr) = $header_depth <= 6
- ? ('h'.$header_depth, '')
- : ('h6', qq{class="h$header_depth"});
- return CloseHtmlEnvironments()
- . AddHtmlEnvironment($CreoleHeaderHtmlTag, $CreoleHeaderHtmlTagAttr);
- }
-
-
-
-
-
- elsif ($CreoleHeaderHtmlTag and m~\G[ \t]*=*[ \t]*(\n|$)~cg) {
- my $header_html =
- CloseHtmlEnvironment($CreoleHeaderHtmlTag, '^'.$CreoleHeaderHtmlTagAttr.'$')
- .AddHtmlEnvironment('p');
- $CreoleHeaderHtmlTag = $CreoleHeaderHtmlTagAttr = '';
- return $header_html;
- }
- return;
- }
- sub CreoleListAndNewLineRule {
- my $is_in_list_item = InElement('li');
-
-
- if (($bol and m/\G[ \t]*([
- ($is_in_list_item and m/\G[ \t]*\n+[ \t]*(
- ($is_in_list_item and m/\G[ \t]*\n+[ \t]*(\*+)[ \t]+/cg)) {
- # Note: the first line of this return statement is --not-- equivalent to:
- # "return CloseHtmlEnvironmentUntil('li')", as that line does not permit
- # modules overriding the CloseHtmlEnvironments() function to "have a say."
- return ($is_in_list_item ? CloseHtmlEnvironmentUntil('li') : CloseHtmlEnvironments())
- .OpenHtmlEnvironment(substr($1, 0, 1) eq '#' ? 'ol' : 'ul', length($1), '', 'ol|ul')
- .AddHtmlEnvironment('li');
- }
- # - bullet list (not nestable; always needs space)
- elsif ($CreoleDashStyleUnorderedLists and (
- ($bol and m/\G[ \t]*(-)[ \t]+/cg) or
- ($is_in_list_item and m/\G[ \t]*\n+[ \t]*(-)[ \t]+/cg))) {
- return ($is_in_list_item ? CloseHtmlEnvironmentUntil('li') : CloseHtmlEnvironments())
- .OpenHtmlEnvironment('ul', length($1))
- .AddHtmlEnvironment ('li');
- }
- # paragraphs: at least two newlines
- elsif (m/\G\s*\n(\s*\n)+/cg) {
- return CloseHtmlEnvironments().AddHtmlEnvironment('p');
- }
- # line break: one newline or explicit "\\"
- #
- # Note, single newlines not matched by this conditional will be converted into
- # a single space. (In general, this is what you want.)
- elsif (($CreoleLineBreaks and m/\G\s*\n/cg) or m/\G\\\\(\s*\n?)/cg) {
- return $q->br();
- }
- return;
- }
- sub GetCreoleImageHtml {
- my $image_html = shift;
- return
- ($bol ? CloseHtmlEnvironments().AddHtmlEnvironment('p', 'class="image"') : '')
- .$image_html;
- }
- sub GetCreoleLinkHtml {
- my ($markup, $html, $link_text) = @_;
- if ($CreoleHeaderHtmlTag) { return $link_text; }
- else {
- Dirty($markup);
- print $html;
- return '';
- }
- }
- *RunMyRulesCreoleOld = \&RunMyRules;
- *RunMyRules = \&RunMyRulesCreole;
- sub RunMyRulesCreole {
-
- my $creole_is_table_cell_bol_last = $CreoleIsTableCellBol;
- $bol = 1 if $CreoleIsTableCellBol;
- my $html = RunMyRulesCreoleOld(@_);
- $CreoleIsTableCellBol = '' if $creole_is_table_cell_bol_last;
- return $html;
- }
- our $CreoleRuleRecursing;
- sub CreoleRuleRecursive {
- my $markup = shift;
- return $markup if $CreoleRuleRecursing;
- local $CreoleRuleRecursing = 1;
- local $bol = 0;
-
- my ($oldpos, $old_) = (pos, $_);
- my @oldHtmlStack = @HtmlStack;
- my @oldHtmlAttrStack = @HtmlAttrStack;
-
- $_ = $markup;
- @HtmlStack = @HtmlAttrStack = ();
- my ($html, $html_creole) = ('', '');
-
-
-
-
- while (1) {
- if ($html_creole = CreoleRule(@_) or
- ($CreoleIsCreoleAddition and
- $html_creole = CreoleAdditionRule(@_))) {
- $html .= $html_creole;
- }
- elsif (m/\G&([a-z]+|#[0-9]+|#x[a-fA-F0-9]+);/cg) {
- $html .= "&$1;";
- }
- elsif (m/\G\s+/cg) {
- $html .= ' ';
- }
- elsif ( m/\G([A-Za-z\x{0080}-\x{fffd}]+([ \t]+[a-z\x{0080}-\x{fffd}]+)*[ \t]+)/cg
- or m/\G([A-Za-z\x{0080}-\x{fffd}]+)/cg
- or m/\G(\S)/cg) {
- $html .= $1;
- }
- else { last; }
- }
-
- @HtmlAttrStack = @oldHtmlAttrStack;
- @HtmlStack = @oldHtmlStack;
- ($_, pos) = ($old_, $oldpos);
-
- $CreoleRuleRecursing = 0;
- return $html;
- }
|