setext

来自「nedit 是一款linux下的开发源码的功能强大的编辑器」· 代码 · 共 1,890 行 · 第 1/5 页

TXT
1,890
字号
Getopt::Long::config( "noignorecase" );GetOptions(     'c=s',  # conditional text definitions, separated by commas    'd',    # do not make titles hypertext references (HTML only)    'D=s',  # specify destination directory for separate HTML files    'h',      \&show_usage,    'H:s',  # create HTML from setext input    'm',    # create NEdit help menu code from setext input    'M=s',  # name NEdit help code files with this suffix    'p',    # same as 'm' but with debug printout    'S:s',  # generate separate HTML files for each subsection    't',    # create text from setext input    'T',    # emit setext typo-tag document    'v=s',    \&declare_variable,    'V',    # emit setext script version information    'w'     # do not emit warning messages.) || &show_usage;#-----------------------------------# Glean only those options specified#-----------------------------------$opt_c && (@cond_text_definitions = split( ",", $opt_c ));$opt_d && ($make_title_href=0);$opt_D && do { $variables{HTML_DIR}=$opt_D; $outputDirectory="$opt_D/" };$opt_h && show_usage();defined $opt_H && do { $convert_to = "html"; getHtmlAttributes( $opt_H ) };$opt_m && do { $make_menu = 1; $convert_to = "help" };$opt_M && ($helpSuffix = $opt_M );$opt_p && do { $make_menu = 1; $convert_to = "help"; $print_menu = 1 };defined $opt_S && do {     $convert_to = "html";    $htmlExt = $opt_S if $opt_S; # user can specify file extension    $separate_html_files=1;     $make_title_href=1     };$opt_t && ($convert_to = "text");$opt_T && emit_setext_definition();$opt_V && (emit_version());$opt_w && ($noWarn = 1);#--------------------------------------------------------------# Setext Parser states.## The names are used to construct "enter_" & "leave_" elements# in the state_change hash table required to be initialized# by language specific initialization routines (see html_init).#--------------------------------------------------------------$FMT     = "fmt";$LIST    = "list";$PRE_FMT = "pre";$QUOTE   = "quote";#----------------------------# Typotag Pattern Definitions#----------------------------$bold_tt       = '\*\*([^\*]+)\*\*([^\*]|$)';$bullet_tt     = '^\* ([^ ])';$empty_line    = '^\s*$';$fld_left      = '\|>';$fld_right     = '<\|';$field_tt      = "(${fld_left}.+?$fld_right)";$field_content = "${fld_left}(.+?)$fld_right";#$field_tt      = "(${fld_left}[^<]+$fld_right)";#$field_content = "${fld_left}([^<]+)$fld_right";$hot_tt        = '\b([\S]*)_\b';$href_tt       = '^\.\.\s+_([\S]*)\s+(.*)\s*';$indent_tt     = '^  ([^ ])';$intHrefMrk    = "#";$internal_href = "^$intHrefMrk(.*)\$";$italic_tt     = '~([^~]*)~';$line_tt       = '^   ---*$';$list_tt       = '^\s*\.([()])';$notouch_tt    = '^!';$passthru_tt   = '^!!';$quote_tt      = '^> ';$section_tt    = '^([1-6])>';$subtitle_tt   = '^---';$suppress_tt   = '^\.\.';$target_tt     = '(?!(^|\s)_[\S]+_(\s|\W|$))(^|\s)_([\S]+)'; # not underline, then target$title_tt      = '^===';$twobuck_tt    = '^\s*\$\$\s*$';$underline_tt  = '\b_([\S]*)_\b';$untouch_tt    = "\\s*(`[^`]+[`'])(?=\\s|\\W|\$)";$variable_def  = '\s*(\w+)\s*([^=]*(=(.*)))?'; # $1 = name, $4 = value$escape_tt     = "@"; # the character escape symbol (need @@ to escape @)$needEscaping  = "$escape_tt(.)";$escapedFound  = "$escMrk(\\d+)$escMrk";if( $make_menu ){    $setext_file = $ARGV[0];    open SETEXT, "<$setext_file" or die "Can't access $setext_file, $OS_ERROR";    make_NEdit_menu_code();}    else  # Global elements for parsing setext{    #-------------------------    # Program option defaults.    #-------------------------    $setext_file    = "-";  # STDIN, allows program to be used as a filter    $converted_file = "-";  # STDOUT    $convert_to     = "text" if $convert_to eq "";        #--------------------------------------    # Begin processing file specifications.    #--------------------------------------    $setext_file = $ARGV[0] if $ARGV[0] ne "";    open SETEXT, "<$setext_file" or die "Can't access $setext_file, $OS_ERROR";    if( $ARGV[1] ne "" )     {         $converted_file = $ARGV[1];        $convert_to = "html" if $converted_file =~ /\.$htmlExt$/; # in case -H forgotten                if( $converted_file eq basename( $converted_file ) )        {            if( $outputDirectory )            {                $converted_file = "$outputDirectory/$converted_file";            }        }    }    open CONVERT, ">$converted_file" or die "Can't create $converted_file, $OS_ERROR";    translate_setext();}#-------------------------------------------------------------------------------#-------------------------------------------------------------------------------#-------------------------------------------------------------------------------sub translate_setext{    #--------------------------------------    # Adding conversion type to conditional    # text definitions for convenience.    #--------------------------------------    push @cond_text_definitions, $convert_to;    get_setext( SETEXT, \@cond_text_definitions, \@data );        extract_menu_info( \@data )       if( $convert_to eq "html" && $separate_html_files );    chomp @data;        # remove the newline character from each line.        register_tt_translationFunctions( $convert_to );    parse_setext( \@data );}#-------------------------------------------------------------------------------sub make_NEdit_menu_code{    #--------------------------------    # Supply a default NEdit version.    #--------------------------------    $neditDefaultMarker   = "NEdit release of ";    $variables{ version } = $neditDefaultMarker . date()        if (not exists $variables{ version } or           $variables{ version } eq "default");        #--------------------------------------    # Adding conversion type to conditional    # text definitions for convenience.    #--------------------------------------    push @cond_text_definitions, $convert_to;    get_setext( SETEXT, \@cond_text_definitions, \@data );    extract_menu_info( \@data );    register_tt_translationFunctions( $convert_to );}#-------------------------------------------------------------------------------sub parse_setext{    my $setextData = shift;        local($crnt_state, $fold, $a, $i, $unt, $lineNo);    $crnt_state    = $FMT;    $lineNo        = -1;    @untouchable   = ();    $fold          = 0;    foreach (@$setextData)     {        $lineNo++;  # current location in data array                #--------------------------        # process title information        #--------------------------        (/$title_tt/i or /$subtitle_tt/i) && do {             &$do_title();             $fold = 0;             next;             };                    /$section_tt/o && do {             &$do_section_tt($1);             $fold = 0;             next;            };                /$passthru_tt/ && do {             &$do_emit_line();            next;            };                next if ( /$suppress_tt/ or /$twobuck_tt/ );        $list_level = 0 if $list_level < 0;  # paranoia protection                #--------------------------------------------------        # handle line breaks, only one empty line gets out.        #--------------------------------------------------        if ( /$empty_line/o ) {            to_state( $FMT );                        if( $list_level and not $fold ) {                &$do_list_tt();                $fold = 1;            }            else {                $fold = &$do_line_break( $fold );            }            next;         }                $fold = 0;  # no more empty lines        /$line_tt/ && do { &$do_line_tt(); next; };                #-----------------------------------        # No change to current state allowed        # during list processing.        #-----------------------------------        if( $list_level == 0 )        {            #------------------            # State transitions            #------------------            if    ( /$quote_tt/o  ) { &to_state( $QUOTE )   }            elsif ( /$bullet_tt/o ) { &to_state( $LIST )    }            elsif ( /$indent_tt/o ) { &to_state( $FMT )     }            elsif ( /$list_tt/o   ) { &to_state( $FMT )     }            else                    { &to_state( $PRE_FMT ) }        }        if( /$notouch_tt/o ) { s/$notouch_tt/ /o; }        else        {            #--------------------------------------------            # Handle the untouchables first.            # Mark their locations for later replacement.            # (see recover_extractions)            #--------------------------------------------            for( $i = scalar( @untouchable ); /$untouch_tt/o; $i++ )            {                $unt = $1;                $unlen = length( $unt );                $unloc = index( $_, $unt );                $untouchable[ $i ] = $unt;                 $front = substr( $_, 0, $unloc );                $back  = substr( $_, $unloc+$unlen );                $_ = $front . $um . $back;             }            &$do_list_tt();            &$do_bullet_tt();            &$do_quote_tt();            &$do_bold_tt();            &$do_italic_tt();            &$do_underline_tt();            &$do_target_tt();            &$do_hot_tt();            &$do_indent_tt();        }        &$do_emit_line();    }        &$do_final();}#-------------------------------------------------------------------------------sub register_tt_translationFunctions{    my $conversion_type = shift;        #----------------------------------------------------    # Register call-back functions for typotag processing    #----------------------------------------------------    $do_bold_tt       = "${conversion_type}_bold_tt";    $do_bullet_tt     = "${conversion_type}_bullet_tt";    $do_emit_line     = "${conversion_type}_emit_line";    $do_final         = "${conversion_type}_final";    $do_hot_tt        = "${conversion_type}_hot_tt";    $do_indent_tt     = "${conversion_type}_indent";    $do_initialize    = "${conversion_type}_init";    $do_italic_tt     = "${conversion_type}_italic_tt";    $do_line_break    = "${conversion_type}_line_break";    $do_line_tt       = "${conversion_type}_line_tt";    $do_list_tt       = "${conversion_type}_list_tt";    $do_quote_tt      = "${conversion_type}_quote_tt";    $do_section_tt    = "${conversion_type}_section_tt";    $do_target_tt     = "${conversion_type}_target_tt";    $do_title         = "${conversion_type}_title";    $do_underline_tt  = "${conversion_type}_underline_tt";    &$do_initialize;  # do any necessary initialization}#-------------------------------------------------------------------------------sub date{    $format = $_[0];        ( $sec,$min,$hour,$mday,$mon,$year,@ignore ) = localtime( time );    $month = (January,February,March,April,May,June,July,              August,September,October,November,December)[$mon];    $year  = $year + 1900;        return $year if $format eq "y";    return "$month $mday, $year" if $format eq "D";    return substr($month,0,3) . " $mday, $year";}#-------------------------------------------------------------------------------sub to_state {    my $given_state = shift;        if ( $crnt_state ne $given_state )     {        if( exists  $state_change{ "leave_$crnt_state"  } )        {            $doStateChange = $state_change{ "leave_$crnt_state"  };            &$doStateChange();        }        if( exists  $state_change{ "enter_$given_state" } )        {            $doStateChange = $state_change{ "enter_$given_state"  };            &$doStateChange();        }        $crnt_state = $given_state;    }}#-------------------------------------------------------------------------------sub count{    my $whatToCount = shift;    my $line        = shift;    my $howMany     = 0;        $howMany++ while( $line =~ /$whatToCount/g );    return $howMany;}#-------------------------------------------------------------------------------sub extract_fields{    local $_ = shift;    local $cond_text_region = shift;    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?