| |
| |
| |
| |
| |
| |
| |
| |
| use strict; |
| use Encode; |
| use IO::Handle(1); |
| STDOUT->autoflush(1); |
| |
| our $highlight = 'D:/Lib/highlight-3.36-x64/highlight.exe'; |
| my $file = encode('gbk', decode('utf8', "D:/Sync/Perl/教学翻译、整理/Perl One-Liners.md")); |
| |
| |
| my @arr; |
| load($file, \@arr); |
| |
| our @sizes = ( 2,3,4,5,6,7,8 ); |
| reset_sizes( \@sizes, \@arr ); |
| format_bbcode(\@arr); |
| |
| my $all = join("\n", @arr); |
| our $table = '[list][table=60%, #f8f8f8][tr][td][font=consolas][size=2]'; |
| our $table_tail = '[/size][/font][/b][/td][/tr][/table][/list]'; |
| format_codeblock(\$all); |
| |
| format_inline(\$all); |
| |
| |
| $all=~s/(\[list\])/$1$1/g; |
| $all=~s/(\[\/list\])/$1$1/g; |
| |
| |
| $all=~s{(\Q[/table]\E.*?\n)\n}{$1}sgi; |
| |
| print encode('gbk', decode("utf8", $all)); |
| |
| sub format_bbcode |
| { |
| my $arr = shift; |
| my $prev; |
| my $curv; |
| my $crlf; |
| my $size; |
| |
| for my $i ( 0 .. $#$arr ) |
| { |
| $arr->[$i] =~s/(\r?\n)$//; |
| $crlf = $1; |
| if ( $arr->[$i]=~s/^(#+)// ) |
| { |
| $curv = length($1); |
| $size = $sizes[$curv-1]; |
| $arr->[$i] = "[size=$size][b]" .$arr->[$i] ."[/b][/size]"; |
| $arr->[$i] .= "[list]"; |
| |
| if ( $prev ) |
| { |
| if ( $curv <= $prev ) |
| { |
| $arr->[$i] = "[/list]"x($prev - $curv + 1) .$arr->[$i]; |
| } |
| elsif ( $curv > $prev ) { } |
| } |
| |
| $prev = $curv; |
| } |
| |
| |
| if ( $i < $#$arr |
| and $arr->[$i+1]=~/^\#/ |
| and ($arr->[$i] ne "") ) |
| { |
| $arr->[$i] .= "\n"; |
| } |
| |
| |
| if ( ($i > 0) |
| and ($arr->[$i] =~/^\s*`/) |
| and ($arr->[$i-1] ne "") |
| and (not $arr->[$i-1]=~/\n$/) |
| and (not $arr->[$i-1]=~/\[list\]/) |
| ) |
| { |
| $arr->[$i-1] .= "\n"; |
| } |
| |
| format_inline( \$arr->[$i] ); |
| format_else(\$arr->[$i]); |
| } |
| |
| |
| $arr->[-1] .= '[/list]'x$prev if ( $prev > 0 ); |
| } |
| |
| sub format_codeblock |
| { |
| our $highlight; |
| my $all = shift; |
| my $bbcode; |
| |
| while ($$all =~/```(\w+)?\r?\n(.*?)\r?\n```/s) |
| { |
| writeFile( "temp.txt", \$2 ); |
| if (defined $1) |
| { `$highlight -K3 -S$1 -OBBCode --no-trailing-nl -ssourceforge ./temp.txt -o bbcode.txt`; } |
| else |
| { `$highlight -K3 -OBBCode --no-trailing-nl -ssourceforge ./temp.txt -o bbcode.txt`; } |
| read_slurp( "bbcode.txt", \$bbcode ); |
| |
| $bbcode = $table .'[font=consolas]' .$bbcode .'[/font]' .$table_tail; |
| $$all =~s/```(\w+)?\r?\n(.*?)```/$bbcode/s; |
| } |
| } |
| |
| sub format_inline |
| { |
| our $highlight; |
| my $line = shift; |
| my ($head, $tail ) = ('[font=consolas][b]', '[/b][/font]'); |
| my $inline_code; |
| |
| if ($$line=~/^\s*`[^"]+("|')(.+)("|')/) |
| { |
| writeFile( "temp.txt", \$2 ); |
| `$highlight -K3 -Sperl -OBBCode --no-trailing-nl -ssourceforge ./temp.txt -o bbcode.txt`; |
| read_slurp( "bbcode.txt", \$inline_code ); |
| $$line=~s/^\s*`([^"]+)("|')(.+)("|')(.*)`/$head$1$2${inline_code}$4$5${tail}/; |
| } |
| } |
| |
| sub format_else |
| { |
| my $line = shift; |
| |
| |
| $$line=~s/\*{2}(.*?)\*{2}/\[b\]$1\[\/b\]/g; |
| |
| $$line=~s/\*(.*?)\*/\[i\]$1\[\/i\]/g; |
| |
| $$line=~s/\[([^\]]+)\]\((.*?)\)/\[url=$2\]$1\[\/url\]/g; |
| |
| } |
| |
| sub reset_sizes |
| { |
| my ($sizes, $arr) = @_; |
| my $max = 0; |
| grep { /^(#+)/; $max = length($1) if (length($1) > $max) } @$arr; |
| @$sizes = reverse @{$sizes}[ 0 .. $max-1 ]; |
| } |
| |
| sub writeFile |
| { |
| my ($f, $ref) = @_; |
| open WRT, ">:raw", $f or warn "$!"; |
| print WRT $$ref; |
| close WRT; |
| } |
| |
| sub read_slurp |
| { |
| my ($f, $ref) = @_; |
| local $/ = undef; |
| open READ,"<:raw:crlf", $f or warn "$!"; |
| $$ref = <READ>; |
| close READ; |
| } |
| |
| sub load |
| { |
| my ($f, $ref) = @_; |
| open READ,"<:raw", $f or warn "$!"; |
| @$ref = <READ>; |
| close READ; |
| }COPY |