[ 新規 | 編集 | 差分 ] [ 検索 | 一覧 | FrontPage ] [ 履歴 (RSS) | 差分履歴 (RSS) ] [ ログイン ]
【PR】Amazon | SL-C3000 | SL-C1000(3/18発売!) | SL-C860 | SL-6000W | SL-6000L | SL-6000N

WalWiki/カスタマイズ/整形ルールの定義

編集

整形ルールをWikiのページで管理する

[format rule definition]

編集

FormatRules の記述例

任意属性値を使った記述例

""(color:#000000)ほげほげ""
生成されるタグ <span style="color:#000000">

""ほげほげ(id:hoge)(size:20pt)""
生成されるタグ <span id="hoge" style="font-size:20pt">ほげほげ</span>

() は任意の位置に書くことができます。

グローバル変数定義

編集
my $FormatRules = 'FormatRules';                            # yakty add [format rule definition]
my %format_rules;                                           # yakty add [format rule definition]
my @format_attrs;                                           # yakty add [format rule definition]

%allow_format_tags 追加

編集

許可するタグの指定

# yakty add [format rule definition] start
my %allow_format_tags = (
    a       => 1,
    b       => 1,
    i       => 1,
    em      => 1,
    del     => 1,
    ins     => 1,
    strong  => 1,
    q       => 1,
    code    => 1,
    span    => 1,
    kbd     => 1,
    samp    => 1,
    var     => 1,
    sup     => 1,
    sub     => 1,
    dfn     => 1,
    cite    => 1,
    tt      => 1,
    big     => 1,
    small   => 1,
);
# yakty add [format rule definition] end

%allow_format_attrs 追加

編集

許可する属性の指定

# yakty add [format rule definition] start
my %allow_format_attrs = (
    class   => 1,
    id      => 1,
    style   => 1,
    cite    => 1,
);
# yakty add [format rule definition] end

main()内の変更

編集
sub main {
    open(LOG, ">>${modifier_dir_data}/log.txt") if ($walrus_debugging >= 2);    # Walrus add [for debug]
    &init_resource;
    &check_modifiers;
    &open_db;
    &init_form;
    &init_InterWikiName;
    &init_AntennaSite;
    &init_FormatRules;   # yakty add [format rule definition]

init_FormatRules()追加

編集
# yakty add [format rule definition] start
sub init_FormatRules
{
    if($database{$FormatRules}){
        my $content = $database{$FormatRules};
        my @txt = split(/\n/, $content);
        foreach (@txt){
            if(/^,\s*(([^a-zA-Z0-9\s])\2+)(<(\w+)([^<>]*)>)[^<>]*(<\/\4>)\1/){
                my ($rule, $mark, $tag, $attr) = ($1, $2, lc($4), $5);
                next if(not $allow_format_tags{$tag});
                my $attrs;
                foreach ($attr =~ m/\w+="[^"]+"/g){
                    /((\w+)="[^"]+")/;
                    next if(not $allow_format_attrs{lc($2)});
                    $attrs .= ' ' . $1;
                }
                $format_rules{$rule} = {
                    tag      => $tag,
                    attr     => $attrs,
                    mark     => $mark,
                };
            } elsif(/^,\s*(\(.+?\))\s*,\s*(\w+)\s*,\s*([^,]+\S)/){
                my ($rule, $name, $value) = ($1, lc($2), $3);
                next if(not $allow_format_attrs{$name});
                $rule =~ s/([()])/\\$1/g;
                $rule =~ s/\$1/\(.+?\)/;
                push(@format_attrs, {
                    rule     => $rule,
                    name     => $name,
                    value    => $value,
                });
            }
        }
    } else {
        # default
        %format_rules = (
            "''"    => { tag => "b", mark => "'", },
            "'''"   => { tag => "i", mark => "'", },
        );
        ;
    }
}
# yakty add [format rule definition] end

extract_attr()追加

編集
# yakty add [format rule definition] start
sub extract_attr {
    my ($textref) = @_;
    my $attr;
    my %attr;
    foreach my $attrref (@format_attrs){
        if($$textref =~ $attrref->{rule}){
            my $val = $1;
            my ($name, $value) = ($attrref->{name}, $attrref->{value});
            $value =~ s/\$1/$val/;
            $value = &escape($value);
            $$textref =~ s/$attrref->{rule}//;
            $attr{$name} .= $value;
            #$attr .= qq( $name="$value");
        }
    }
    foreach (keys %attr){
    	$attr .= qq( $_="$attr{$_}");
    }
    return $attr;
}
# yakty add [format rule definition] end

format_text()追加

編集
# yakty add [format rule definition] start
sub format_text {
    my ($rule, $text) = @_;
    my ($tag, $attr) = ($format_rules{$rule}{tag}, $format_rules{$rule}{attr});
#    foreach my $attrref (@format_attrs){
#        if($text =~ $attrref->{rule}){
#            my $val = $1;
#            my ($name, $value) = ($attrref->{name}, $attrref->{value});
#            $value =~ s/\$1/$val/;
#            $value = &escape($value);
#            $text =~ s/$attrref->{rule}//;
#            $attr .= qq( $name="$value");
#        }
#    }
    $attr .= &extract_attr(\$text);
    return qq(<$tag$attr>$text</$tag>);
}
# yakty add [format rule definition] end

inline()内の変更

編集
sub inline {
    my ($line) = @_;
    $line = &escape($line);
#   $line =~ s|'''([^']+?)'''|<i>$1</i>|g;  # Italic        # Walrus del [six single quotes]
#   $line =~ s|''([^']+?)''|<b>$1</b>|g;    # Bold          # Walrus del [six single quotes]
    # yakty add [format rule definition] start
    foreach my $rule (sort {length($b) <=> length($a)} keys %format_rules){
        my $rulestr = &escape($rule);
        my $mark = &escape($format_rules{$rule}{mark});
        #$line =~ s|(?<!\Q$mark\E)\Q$rulestr\E(?!\Q$mark\E)(.+?)(?<!\Q$mark\E)\Q$rulestr\E(?!\Q$mark\E)|&format_text($rule, $1)|ge;
        $line =~ s|\Q$rulestr\E(?!\Q$mark\E)(.+?)(?<!\Q$mark\E)\Q$rulestr\E|&format_text($rule, $1)|ge;
    }
    # yakty add [format rule definition] end
    $line =~ s|(\d\d\d\d-\d\d-\d\d \(\w\w\w\) \d\d:\d\d:\d\d)|<span class="date">$1</span>|g;   # Date
    $line =~ s!
                (
                    ((mailto|http|https|ftp):([^\x00-\x20()<>\x7F-\xFF])*)  # Direct http://...
                        |
                    ($bracket_name)             # [[likethis]], [[#comment]], [[Friend:remotelink]]
                        |
                    ($interwiki_definition)     # [[Friend http://somewhere/?q=sjis($1)]]
                        |
                    ($wiki_name)                # LocalLinkLikeThis
                        |                       # yakty add [auto link]
                    ($auto_link_name)           # yakty add [auto link]
                )
                (?\![^<]*>)                    # yakty add [format rule definition]
            !
#               &make_link($1)                  # Walrus del [link cache]
#                (defined($link_cache{$1})) ? $link_cache{$1} : (length($_ = &make_link($1)) < 1024) ? ($link_cache{$1} = $_) : $_ # Walrus add [link cache] # yakty del [make cached link]
                &make_cachedlink($1)            # yakty add [make cached link]
            !gex;

    # Walrus add [six single quotes] start
    $line =~ s|''''''||g;                   # six single quotes
    # yakty del [format rule definition] start
#    $line =~ s|'''(.+?)'''|<i>$1</i>|g;     # Italic
#    $line =~ s|''(.+?)''|<b>$1</b>|g;       # Bold
    # yakty del [format rule definition] end
    # Walrus add [six single quotes] end
    return $line;
}

remove_modification()内の変更

編集
# Walrus add [remove modification] start
sub remove_modification {
    my ($text, $remove_block_simbol) = @_;
    $text =~ s/^(\*+|-+|:)?\s*// if ($remove_block_simbol);
    $text =~ s/\[\[(\S+)( \S+)?\]\]/$1/g;
    # yakty del [format rule definition] start
    #$text =~ s/''+//g;
    #$text =~ s/``+//g;
    # yakty del [format rule definition] end
    # yakty add [format rule definition] start
    foreach my $rule (sort {length($b) <=> length($a)} keys %format_rules){
    	$text =~ s/\Q$rule\E//g;
    }
    # yakty add [format rule definition] end
    return $text;
}
# Walrus add [remove modification] end

match_text()内の変更

編集
    foreach my $line (split(/\x0D\x0A|\x0D|\x0A/, $text)) {
        unless ($line =~ s/^[ \t]//) {
            #$line =~ s/'{2,}//g;    # yakty del [format rule definition]
            # yakty add [format rule definition] start
            foreach my $rule (sort {length($b) <=> length($a)} keys %format_rules){
                $line =~ s/\Q$rule\E//g;
            }
            # yakty add [format rule definition] end
            push(@links, $2) while ($line =~ s/$interwiki_definition/$1/);
            $line =~ s/$bracket_name/$1/g;
            $line =~ s/^[:,\*-]+ *//;
            $line =~ s/\s+/ /g;
        }
        push(@lines, $line." ");
    }

03695