Changeset 213

Show
Ignore:
Timestamp:
04/06/07 20:38:39 (2 years ago)
Author:
ingy
Message:
 r3761@dhcp199:  ingy | 2007-04-05 12:56:10 +0900
 more parser refo
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/core/Spork/lib/Spork/Parser.pm

    r212 r213  
    11package Spork::Parser; 
     2use strict; 
     3use warnings; 
    24use base 'Document::Parser'; 
    3  
    4 my $ALPHANUM = '\p{Letter}\p{Number}\pM'; 
    5  
    6 #------------------------------------------------------------------------------- 
    7 # Public - $parsed = $parser->parse($wikitext); 
    8 #------------------------------------------------------------------------------- 
    9 sub parse { 
    10     my $self = shift; 
    11     $self->set_grammar; 
    12     $self->set_ast; 
    13     return $self->SUPER::parse(@_); 
    14 } 
    155 
    166sub set_grammar { 
     
    3020            blocks => $all_blocks, 
    3121            match => qr/^((?m:^>+.*\n)+\n?)/, 
     22            filter => sub { s/^> *//mg; s/\n+\z/\n/ }, 
    3223        }, 
    3324        pre => { 
    3425            match => qr/^(( +.*\S.*\n)+)(?m:^ *\n)*/, 
     26            filter => sub { while (not /^\S/m) { s/^ //gm } }, 
    3527        }, 
    3628        p => { 
    3729            phrases => $all_phrases, 
    3830            match => qr/^(((?!>).*\S.*\n)+)(?m:^\s*\n)*/, 
     31            filter => sub { s/\n+\z// }, 
    3932        }, 
    4033        ul => { 
    4134            blocks => [qw(ul li)], 
    4235            match => qr/^((?m:^\*+ .*\n)+)\n*/, 
     36            filter => sub { s/^\* *//mg; s/\n+\z/\n/; }, 
    4337        }, 
    4438        li => { 
     
    6660} 
    6761 
     62my $ALPHANUM = '\p{Letter}\p{Number}\pM'; 
     63 
    6864sub re_huggy { 
    6965    my $brace1 = shift; 
     
    8076    my $self = shift; 
    8177    $self->{ast} = Spork::AST->new; 
    82 } 
    83  
    84 sub handle_indent { 
    85     my $self = shift; 
    86     $self->subparse(parse_blocks => @_, sub { 
    87         s/^> *//mg; 
    88         s/\n+\z/\n/; 
    89     }); 
    90 } 
    91  
    92 sub handle_center { 
    93     my $self = shift; 
    94     $self->subparse(parse_blocks => @_); 
    95 } 
    96  
    97 sub handle_h2 { 
    98     my $self = shift; 
    99     $self->subparse(parse_phrases => @_); 
    100 } 
    101  
    102 sub handle_p { 
    103     my $self = shift; 
    104     $self->subparse(parse_phrases => @_, sub { s/\n+\z// }); 
    105 } 
    106  
    107 sub handle_pre { 
    108     my $self = shift; 
    109     $self->subparse(parse_phrases => @_, sub { 
    110         while (not /^\S/m) { 
    111             s/^ //gm; 
    112         } 
    113     }); 
    114 } 
    115  
    116 sub handle_ul { 
    117     my $self = shift; 
    118     $self->subparse(parse_blocks => @_, sub { 
    119         s/^\* *//mg; 
    120         s/\n+\z/\n/; 
    121     }); 
    122 } 
    123  
    124 sub handle_li { 
    125     my $self = shift; 
    126     $self->subparse(parse_phrases => @_); 
    127 } 
    128  
    129 sub handle_hilite { 
    130     my $self = shift; 
    131     $self->subparse(parse_phrases => @_); 
    132 } 
    133  
    134 sub handle_b { 
    135     my $self = shift; 
    136     $self->subparse(parse_phrases => @_); 
    137 } 
    138  
    139 sub handle_tt { 
    140     my $self = shift; 
    141     $self->subparse(parse_phrases => @_); 
    142 } 
    143  
    144 sub handle_i { 
    145     my $self = shift; 
    146     $self->subparse(parse_phrases => @_); 
    14778} 
    14879 
  • trunk/src/plugins/ingy/Document-Parser/lib/Document/Parser.pm

    r212 r213  
    1818    $self->{input} ||= shift; 
    1919    $self->{grammar} or $self->set_grammar; 
    20     $self->{ast} or $self->set_grammar
     20    $self->{ast} or $self->set_ast
    2121    $self->parse_blocks('top'); 
    2222    return $self->{ast}->content; 
     
    109109    my ($self, $type, $match) = @_; 
    110110    my $func = "handle_$type"; 
    111     $self->$func($match, $type); 
    112 
    113  
    114 # sub handle_p { 
    115 #     my $self = shift; 
    116 #     $self->subparse(p => parse_phrases => @_, sub { s/\n+\z// }); 
    117 # } 
    118  
     111    if ($self->can($func)) { 
     112        $self->$func($match, $type); 
     113    } 
     114    else { 
     115        my $grammar = $self->{grammar}{$type}; 
     116        my $parse = $grammar->{blocks} 
     117        ? 'parse_blocks' 
     118        : 'parse_phrases'; 
     119        my @filter = $grammar->{filter} 
     120        ? ($grammar->{filter}) 
     121        : (); 
     122        $self->subparse($parse, $match, $type, @filter); 
     123    } 
     124
    119125 
    120126sub subparse {