| 1 | package Text::Wikispaces2Markdown; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = "0.1"; | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | Text::Wikispaces2Markdown - convert wikispaces pages to markdown | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | Do a rough conversion of Wikispaces.com markup into Markdown. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head2 convert | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | Does the conversion using dumb regexp rules. Would fare better with a proper parser. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub convert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | shift if ( $_[0] eq __PACKAGE__ ); # oops, called in OOP fashion. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # Paramaters: | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my $text = shift; # text to be parsed | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | my @lines = split /\n/, $text; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | for my $i (0..$#lines) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 65 | $_ = $lines[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 31 | # convert links | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 65 | $lines[$i] =~ s/\[\[(.*?)\|(.*?)]]/[$2]($1)/g; | |||||||||||||||||||||||||||||||||||||||||
| 33 | # convert italic | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 65 | $lines[$i] =~ s{(?<!:)//}{_}g; # FIXME: very crude avoidance of URLs; will break in code blocks | |||||||||||||||||||||||||||||||||||||||||
| 35 | # convert ToC | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 65 | $lines[$i] =~ s/\Q[[toc]]/{{toc}}/g; | |||||||||||||||||||||||||||||||||||||||||
| 37 | # convert nested lists | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 65 | $lines[$i] =~ s/^([#*])\1+/(' ' x length $&) . $1/meg; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # convert ordered lists | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 65 | $lines[$i] =~ s/^(\s*)#/$1 . '1.'/meg; | |||||||||||||||||||||||||||||||||||||||||
| 42 | # add a line before lists, as (annoyingly) required by Markdown | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 65 | ▶ | if ($lines[$i] =~ /^[*0-9]/ and $i > 0 and $lines[$i-1] !~ /^\s*$|^\s*[*0-9]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$lines[$i] =~ /^[*0-9]/ and $i > 0 and not $lines[$i - 1] =~ /^\s*$|^\s*[*0-9]/
$lines[$i] =~ /^[*0-9]/ and $i > 0
MC/DC66%2/3
$lines[$i] =~ /^[*0-9]/ and $i > 0 and not $lines[$i - 1] =~ /^\s*$|^\s*[*0-9]/
$lines[$i] =~ /^[*0-9]/
$i > 0
not $lines[$i - 1] =~ /^\s*$|^\s*[*0-9]/
Truth table75%3/4
A and B and not(C)
A= $lines[$i] =~ /^[*0-9]/B= $i > 0C= not $lines[$i - 1] =~ /^\s*$|^\s*[*0-9]/
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | substr($lines[$i], 0, 0, "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # convert headings | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 65 | ▶ | if ($lines[$i] =~ s/^(=+)(.+?)=*$/('#' x length $1) . ' ' . $2/me) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | # make sure headings are preceded by a blank line | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | ▶ | substr($lines[$i], 0, 0, "\n") if $i > 0 and $lines[$i-1] !~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i > 0 and not $lines[$i - 1] =~ /^\s*$/
MC/DC100%2/2
$i > 0 and not $lines[$i - 1] =~ /^\s*$/
$i > 0
not $lines[$i - 1] =~ /^\s*$/
Truth table100%3/3
A and not(B)
A= $i > 0B= not $lines[$i - 1] =~ /^\s*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | # remove explicit anchors to headings (e.g. '=[[#Reliability]] Reliability='; assumes you run a {{toc}}) | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # $lines[$i] =~ s/\[\[#(.*?)]] \1/[[$1]]/; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # remove anchors anwyay - they seem to be an odd artefact of Wikispaces. Be careful in the strange case in which anchors are named something other than the heading's 'a' name | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | $lines[$i] =~ s/\[\[#(.*?)]] (.*)/$2/; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | return (join "\n", @lines) . ($text =~ /(\n+)\z/? $1 : ''); # append the last \n if any, which would be lost by the initial split | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | Dan Dascalescu (dandv), L<http://dandascalescu.com> | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =head1 License | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | This module is licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||