| 1 | package Pod::Simple::Wiki::Markdown; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | ############################################################################### | ||||||||||||||||||
| 4 | # | ||||||||||||||||||
| 5 | # Pod::Simple::Wiki::Markdown - A class for creating Pod to Markdown filters. | ||||||||||||||||||
| 6 | # | ||||||||||||||||||
| 7 | # | ||||||||||||||||||
| 8 | # Copyright 2003-2014, John McNamara, jmcnamara@cpan.org, Daniel T. Staal, | ||||||||||||||||||
| 9 | # DStaal@usa.net | ||||||||||||||||||
| 10 | # | ||||||||||||||||||
| 11 | # Documentation after __END__ | ||||||||||||||||||
| 12 | # | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | # perltidy with the following options: -mbl=2 -pt=0 -nola | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 5 | ▶ | use Pod::Simple::Wiki; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | 5 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | 5 | ▶ | use vars qw(@ISA $VERSION); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 5 | @ISA = qw(Pod::Simple::Wiki); | |||||||||||||||||
| 22 | 5 | $VERSION = '0.20'; | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | ############################################################################### | ||||||||||||||||||
| 25 | # | ||||||||||||||||||
| 26 | # The tag to wiki mappings. | ||||||||||||||||||
| 27 | # | ||||||||||||||||||
| 28 | 5 | my $tags = { | |||||||||||||||||
| 29 | '<b>' => '**', | ||||||||||||||||||
| 30 | '</b>' => '**', | ||||||||||||||||||
| 31 | '<i>' => '_', | ||||||||||||||||||
| 32 | '</i>' => '_', | ||||||||||||||||||
| 33 | '<tt>' => '`', | ||||||||||||||||||
| 34 | '</tt>' => '`', | ||||||||||||||||||
| 35 | '<pre>' => "\n```\n", | ||||||||||||||||||
| 36 | '</pre>' => "\n```\n", | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | '<h1>' => '# ', | ||||||||||||||||||
| 39 | '</h1>' => "\n\n", | ||||||||||||||||||
| 40 | '<h2>' => '## ', | ||||||||||||||||||
| 41 | '</h2>' => "\n\n", | ||||||||||||||||||
| 42 | '<h3>' => '### ', | ||||||||||||||||||
| 43 | '</h3>' => "\n\n", | ||||||||||||||||||
| 44 | '<h4>' => '#### ', | ||||||||||||||||||
| 45 | '</h4>' => "\n\n", | ||||||||||||||||||
| 46 | }; | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | ############################################################################### | ||||||||||||||||||
| 49 | # | ||||||||||||||||||
| 50 | # new() | ||||||||||||||||||
| 51 | # | ||||||||||||||||||
| 52 | # Simple constructor inheriting from Pod::Simple::Wiki. | ||||||||||||||||||
| 53 | # | ||||||||||||||||||
| 54 | 30 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 30 | my $class = shift; | |||||||||||||||||
| 57 | 30 | my $self = Pod::Simple::Wiki->new( 'wiki', @_ ); | |||||||||||||||||
| 58 | 30 | $self->{_tags} = $tags; | |||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 30 | bless $self, $class; | |||||||||||||||||
| 61 | 30 | return $self; | |||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | ############################################################################### | ||||||||||||||||||
| 65 | # | ||||||||||||||||||
| 66 | # _indent_item() | ||||||||||||||||||
| 67 | # | ||||||||||||||||||
| 68 | # Indents an "over-item" to the correct level. | ||||||||||||||||||
| 69 | # | ||||||||||||||||||
| 70 | 37 | ▶ | sub _indent_item { | ||||||||||||||||
|
Subroutine
_indent_item: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 37 | my $self = shift; | |||||||||||||||||
| 73 | 37 | my $item_type = $_[0]; | |||||||||||||||||
| 74 | 37 | my $item_param = $_[1]; | |||||||||||||||||
| 75 | 37 | my $indent_level = $self->{_item_indent} - 1; | |||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 37 | ▶ | if ( $item_type eq 'bullet' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 78 | 12 | $self->_append( ' ' x $indent_level . '* ' ); | |||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | elsif ( $item_type eq 'number' ) { | ||||||||||||||||||
| 81 | 12 | $self->_append( ' ' x $indent_level . '1 ' ); | |||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | # In theory Markdown supports nested definition lists - but *everything* has to be indented. | ||||||||||||||||||
| 85 | elsif ( $item_type eq 'text' ) { | ||||||||||||||||||
| 86 | 13 | $self->_append( ' ' x $indent_level . '' ); | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | ############################################################################### | ||||||||||||||||||
| 91 | # | ||||||||||||||||||
| 92 | # _start_L() | ||||||||||||||||||
| 93 | # | ||||||||||||||||||
| 94 | # Handle the start of a link element. | ||||||||||||||||||
| 95 | # | ||||||||||||||||||
| 96 | 4 | ▶ | sub _start_L { | ||||||||||||||||
|
Subroutine
_start_L: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 4 | my $self = shift; | |||||||||||||||||
| 99 | 4 | my $link_attrs = shift; | |||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 4 | $self->{_link_attrs} = $link_attrs; | |||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | # Ouput start of Confluence link and flush the _wiki_text buffer. | ||||||||||||||||||
| 104 | 4 | $self->_output( '[' ); | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | ############################################################################### | ||||||||||||||||||
| 109 | # | ||||||||||||||||||
| 110 | # _end_L() | ||||||||||||||||||
| 111 | # | ||||||||||||||||||
| 112 | # Handle the end of a link element. | ||||||||||||||||||
| 113 | # | ||||||||||||||||||
| 114 | 4 | ▶ | sub _end_L { | ||||||||||||||||
|
Subroutine
_end_L: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 4 | my $self = shift; | |||||||||||||||||
| 117 | 4 | my $link_attrs = $self->{_link_attrs}; | |||||||||||||||||
| 118 | 4 | my $link_target = $link_attrs->{to}; | |||||||||||||||||
| 119 | 4 | my $link_section = $link_attrs->{section}; | |||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | 4 | ▶ | $link_target = '' if( !defined($link_target)); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | # Handle links that are parsed as Pod links. | ||||||||||||||||||
| 124 | 4 | ▶ | if ( defined $link_section ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 125 | 2 | $link_target = "$link_target#$link_section"; | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 4 | $self->_append( "]($link_target)" ); | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | ############################################################################### | ||||||||||||||||||
| 132 | # | ||||||||||||||||||
| 133 | # _handle_text() | ||||||||||||||||||
| 134 | # | ||||||||||||||||||
| 135 | # Perform any necessary transforms on the text. This is mainly used to escape | ||||||||||||||||||
| 136 | # inadvertent CamelCase words. | ||||||||||||||||||
| 137 | # | ||||||||||||||||||
| 138 | 69 | ▶ | sub _handle_text { | ||||||||||||||||
|
Subroutine
_handle_text: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 69 | my $self = shift; | |||||||||||||||||
| 141 | 69 | my $text = $_[0]; | |||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | # Only escape words in paragraphs | ||||||||||||||||||
| 144 | 69 | ▶ | if ( not $self->{_in_Para} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 145 | 41 | $self->{_wiki_text} .= $text; | |||||||||||||||||
| 146 | 41 | return; | |||||||||||||||||
| 147 | } | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | # Split the text into tokens but maintain the whitespace | ||||||||||||||||||
| 150 | 28 | my @tokens = split /(\s+)/, $text; | |||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | # Escape any tokens here, if necessary. | ||||||||||||||||||
| 153 | # The following characters are escaped by prepending a backslash: \`*_ | ||||||||||||||||||
| 154 | # (Markdown has other escapes as well, but these cover most cases, and the others | ||||||||||||||||||
| 155 | # are usually optional.) | ||||||||||||||||||
| 156 | 28 | @tokens = map { s/([\\`\*\_])/\\$1/g; $_ } @tokens; | |||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | # Rejoin the tokens and whitespace. | ||||||||||||||||||
| 159 | 28 | $self->{_wiki_text} .= join '', @tokens; | |||||||||||||||||
| 160 | } | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | ############################################################################### | ||||||||||||||||||
| 164 | # | ||||||||||||||||||
| 165 | # Functions to deal with =over ... =back regions for | ||||||||||||||||||
| 166 | # | ||||||||||||||||||
| 167 | # Bulleted lists | ||||||||||||||||||
| 168 | # Numbered lists | ||||||||||||||||||
| 169 | # Text lists | ||||||||||||||||||
| 170 | # Block lists | ||||||||||||||||||
| 171 | # | ||||||||||||||||||
| 172 | 13 | ▶ | sub _end_item_text { | ||||||||||||||||
|
Subroutine
_end_item_text: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 173 | 13 | my $self = shift; | |||||||||||||||||
| 174 | 13 | my $indent_level = $self->{_item_indent} - 1; | |||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | 13 | $self->_output( "\n" . ' ' x $indent_level . ': ' ); | |||||||||||||||||
| 177 | } | ||||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | ############################################################################### | ||||||||||||||||||
| 181 | # | ||||||||||||||||||
| 182 | # _start_Para() | ||||||||||||||||||
| 183 | # | ||||||||||||||||||
| 184 | # Special handling for paragraphs that are part of an "over" block. | ||||||||||||||||||
| 185 | # | ||||||||||||||||||
| 186 | 27 | ▶ | sub _start_Para { | ||||||||||||||||
|
Subroutine
_start_Para: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | 27 | my $self = shift; | |||||||||||||||||
| 189 | 27 | my $indent_level = $self->{_item_indent} - 1; | |||||||||||||||||
| 190 | |||||||||||||||||||
| 191 | 27 | ▶ | if ( $self->{_in_over_block} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 192 | 1 | $self->_append( ' ' x $indent_level . '' ); | |||||||||||||||||
| 193 | } | ||||||||||||||||||
| 194 | } | ||||||||||||||||||
| 195 | |||||||||||||||||||
| 196 | |||||||||||||||||||
| 197 | 5 | 1; | |||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||