FunctionalPerl
view release on metacpan or search on metacpan
htmlgen/FunctionalPerl/Htmlgen/Mediawiki.pm view on Meta::CPAN
#
# Copyright (c) 2014-2023 Christian Jaeger, copying@christianjaeger.ch
#
# This is free software, offered under either the same terms as perl 5
# or the terms of the Artistic License version 2 or the terms of the
# MIT License (Expat version). See the file COPYING.md that came
# bundled with this file.
#
=head1 NAME
FunctionalPerl::Htmlgen::Mediawiki
=head1 SYNOPSIS
=head1 DESCRIPTION
Expand `[[ ]]` in markdown source text into standard markdown format.
=head1 NOTE
This is alpha software! Read the status section in the package README
or on the L<website|http://functional-perl.org/>.
=cut
package FunctionalPerl::Htmlgen::Mediawiki;
use strict;
use warnings;
use warnings FATAL => 'uninitialized';
use experimental "signatures";
use Sub::Call::Tail;
use Exporter "import";
our @EXPORT = qw();
our @EXPORT_OK = qw(mediawiki_prepare mediawiki_replace mediawiki_rexpand
mediawiki_expand);
our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
use FP::Docstring;
use Chj::chompspace;
use Chj::TEST ":all";
use PXML::XHTML ":all";
use FP::Show;
use URI;
# and for text display we need to *decode* URIs..
# COPY from chj-bin's `urldecode`, now modified by having our own
# uri_escape
use Encode;
# Adapted copy from URI::Escape
sub uri_unescape($str) {
# Note from RFC1630: "Sequences which start with a percent sign
# but are not followed by two hexadecimal characters are reserved
# for future extension"
$str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $str;
$str;
}
sub url_decode {
my ($str) = @_;
my $str2 = uri_unescape($str);
decode("utf-8", $str2, Encode::FB_CROAK)
}
# Hack to avoid failing tests because of changed URI behaviour. The
# new URI behaviour is probably the correct one but don't want to fail
# with older versions.
sub possibly_unescape_brackets {
my ($str) = @_;
# warn "URI $URI::VERSION";
$URI::VERSION < 5.11
? do {
$str =~ s{\%5B}{\[}sg;
$str =~ s{\%5D}{\]}sg;
$str
}
: $str
}
# escape [ ] for markdown; XX is this correct?
sub squarebracked_escape($str) {
$str =~ s%([\[\]])%\\$1%sg;
$str
}
# meant to preprocess the whole markdown document; keep the returned
# table and the used token for mediawiki_expand.
sub mediawiki_prepare ($str, $token) {
__ '($str, $tokenstr) -> ($str, $hashtable) '
. '-- replace "[[...]]" syntax in $str with replacement tokens '
. 'which use $tokenstr as prefix';
my $table = {};
my $n = 0;
$str =~ s%(^|.)\[\[(.*?[^\\])\]\]%
my ($pre,$cont) = ($1,$2);
$n++;
$$table{$n} = $cont;
$pre.$token."-".$n."-" # must be inert to markdown parser
( run in 1.132 second using v1.01-cache-2.11-cpan-0b5f733616e )