| 1 | ############################################################################# | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # (c) by Tels 2004. Part of Convert::Wiki | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # contains the from_txt() fuctionality for Convert::Wiki | ||||||||||||||||||||||||||||||||||||||||||
| 5 | ############################################################################# | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | package Convert::Wiki; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | sub _from_txt | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_from_txt: called CC=30 SCAR=40.6
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | my ($self,$txt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | $self->clear(); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | ######################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # Stage 0: global normalization | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # convert "\n \n" to "\n\n" | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | $txt =~ s/\n\s+\n/\n\n/; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # convert "foo:\nbah" to "foo:\n\nbah" (but not headlines ending in ":") | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | $txt =~ s/:\s*\n([^=_-])/:\n\n$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # remove leading \n: | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | $txt =~ s/^\n+//g; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # remove leading lines: | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | $txt =~ s/^\s*[=_-]+\n+//; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # take the text, recognize parts at it's beginning until we no longer have | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # anything left | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | my ($opt); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | my $tries = 0; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | my $node_nr = 0; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | my $last_node = $self->{nodes}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | while ((length($txt) > 0) && ($tries++ < 16)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $txt > 0 and $tries++ < 16
MC/DC50%1/2
length $txt > 0 and $tries++ < 16
length $txt > 0
$tries++ < 16
Truth table66%2/3
A and B
A= length $txt > 0B= $tries++ < 16
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | ######################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # Stage 1: local normalization | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # remove superflous newlines | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 47 | $txt =~ s/^\n+//g; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # remove "=========" and similiar stray delimiters | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 47 | $txt =~ s/^[=]+\n+//; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | ######################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # Stage 2: conversion to internal format | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 47 | $opt = undef; # reset | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 47 | ▶ | if ($self->{debug}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $txt =~ /^(.*)\n(.*)\n(.*)/; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $a = $1 || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$1 || ''
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $b = $2 || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$2 || ''
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $c = $3 || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$3 || ''
MC/DC0%0/1
$3 || ''
$3
Truth table0%0/2
A
A= $3
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | print STDERR "# at node $node_nr\n"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | print STDERR "# Text start is now:\n# '$a'\n# '$b'\n# '$c'\n"; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # "Foo\n===" looks like a headline | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 47 | ▶ | if ($txt =~ s/^([^=_-].+)\n[=_-]+\n+//) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 9 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | print STDERR "# case 1\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 13 | $opt = { txt => $1, type => 'head1' }; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # '-----' or '_____' to rulers | ||||||||||||||||||||||||||||||||||||||||||
| 72 | elsif ($txt =~ s/^[-_]+\n//) | ||||||||||||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | print STDERR "# case 2\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | $opt = { type => 'line' }; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # "1. Foo\n" looks like a bullet | ||||||||||||||||||||||||||||||||||||||||||
| 78 | elsif ($txt =~ s/^([\d\.]+) (.+)\n//) | ||||||||||||||||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | print STDERR "# case 3\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $opt = { txt => $2, name => $1, type => 'item' }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # "* Foo\n" looks like a bullet | ||||||||||||||||||||||||||||||||||||||||||
| 84 | elsif ($txt =~ s/^(\s*[*+-](\s+(.|\n)+?))(\n\n|\n\s*[*+-])/$4/) | ||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | ▶ | print STDERR "# case 4\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | my $t = $1; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | $t =~ s/^\s*[*+-]\s+//; # "- Foo" => "Foo" | |||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo" | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 7 | $t =~ s/\s+\z//g; # remove trailing space | |||||||||||||||||||||||||||||||||||||||||
| 92 | 7 | $t =~ s/\n/ /g; # remove newlines entirely | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | $opt = { txt => $t, type => 'item' }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # " some text\nmore text" is one paragraph and not monospaced | ||||||||||||||||||||||||||||||||||||||||||
| 97 | elsif ($txt =~ s/^\s+(([^\s].+\n){2,})//) | ||||||||||||||||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | print STDERR "# case 5\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | $opt = { txt => $1, type => 'para' }; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # " some text\n\n" is one monospaced line | ||||||||||||||||||||||||||||||||||||||||||
| 103 | elsif ($txt =~ s/^\s+(((.+)\n\n))//) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | print STDERR "# case 6\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | my $t = $1; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo" | |||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | $t =~ s/\n\z//g; # remove trailing \n | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | $opt = { txt => $t, type => 'mono' }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # " Foo\n" looks like a monospaced text | ||||||||||||||||||||||||||||||||||||||||||
| 113 | elsif ($txt =~ s/^(([ \t]+[^\s\n*+=-].+\n){2,})//) | ||||||||||||||||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | print STDERR "# case 7 '$1'\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $t = $1; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $t =~ s/^\s+//g; # " Boo" => "Boo" | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo" | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $t =~ s/\n\z//g; # remove trailing \n | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $opt = { txt => $t, type => 'mono' }; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # Also: "$ Foo\n" and "# bah" look like a monospaced text | ||||||||||||||||||||||||||||||||||||||||||
| 125 | elsif ($txt =~ s/^([\$\#])\s+(((.+)\n){1,})//) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | print STDERR "# case 8\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | my $t = "$1 $2"; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo" | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | $t =~ s/\n\z//g; # remove trailing \n | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | $opt = { txt => $t, type => 'mono' }; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # "Foo\n" looks like a text | ||||||||||||||||||||||||||||||||||||||||||
| 135 | elsif ($txt =~ s/^([^\s](([^*+\n-].+)\n){1,})//) | ||||||||||||||||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 18 | ▶ | print STDERR "# case 9\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 18 | $opt = { txt => $1, type => 'para' }; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 47 | ▶ | if (defined $opt) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 45 | $tries = 0; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 45 | ▶ | if ($self->{debug}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | print STDERR Data::Dumper::Dumper($opt); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 45 | $opt->{interlink} = $self->{interlink}; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 45 | my $node = Convert::Wiki::Node->new( $opt ); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 45 | ▶ | if ($last_node) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # link node to last node | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 38 | $last_node->link( $node ); | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | else | ||||||||||||||||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 7 | $self->{nodes} = $node; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 45 | $last_node = $node; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 45 | $node_nr++; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 7 | ▶ | if ($tries > 0 && length($txt) > 0) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tries > 0 and length $txt > 0
MC/DC0%0/2
$tries > 0 and length $txt > 0
$tries > 0
length $txt > 0
Truth table66%2/3
A and B
A= $tries > 0B= length $txt > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # something was left over | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $self->error( "Cannot recognize text ahead of me. Giving up." ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||||||||||||||||