Thread editor in perl 0.3.n: hurra (630 answers)
Opened by zipster at 2005-10-21 17:46

renee
 2007-11-28 15:24
#103230 #103230
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Ich habe mal wieder etwas rumgespielt:

Kephra::App::Menu :
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        if ($menu_name eq '&insert_templates') {
                set_update($menu_id, sub {
                        my $cfg = $Kephra::config{file}{templates}; 
                        my $file = Kephra::Config::filepath($cfg->{directory}, $cfg->{file});
                        my $tmp = Kephra::Config::File::load( $file );
                        my @menu_data;
                        if (exists $tmp->{template}){
                                $tmp = Kephra::Config::Tree::_convert_node_2_AoH(\$tmp->{template});
                                for my $template ( @{$tmp} ) {
                                        my %item;
                                        $item{type} = 'item';
                                        $item{label}= $template->{name};
                                        $item{call} = sub {
                                                my $text = $template->{content};
                                                if( exists $template->{preprocess} ){
                                                        my $module = $template->{preprocess};
                                                        (my $file  = $module) =~ s!::!/!g;
                                                        $file      = $module . '.pm';
                                                        eval{
                                                                require $file;
                                                                my $sub = $module->can( 'preprocess' );
                                                                if( $sub ){
                                                                        $text = $sub->( $text );
                                                                }
                                                        };
                                                }
                                                Kephra::Edit::insert_text( $text );
                                        };
                                        $item{help}= $template->{description};
                                        push @menu_data, \%item; 
                                }
                        }
                        eval_data($menu_id, \@menu_data);
                });

                set_absolete($menu_id);

        }


Dann kann man in den Templates so etwas schreiben:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
<template>
name = Skript
description = Skript-Stub
preprocess = ReneeB
content = <<EOF
#!/usr/bin/perl

use strict;
use warnings;

my $filename = "$(file)";
EOF
</template>


In dem Modul ReneeB:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package ReneeB;

use strict;
use warnings;

sub preprocess{
        my ($text) = @_;
        
        my $file = Kephra::Document::_get_current_name();
        $text =~ s~(?!\\)\$\(file\)~$file~g;
        
        return $text;
}

1;
Und man ist nicht auf ein paar Variablen beschränkt und kommt sich nicht mit anderen Sprachen in die Quere.

Hintergrund warum ich das gemacht habe:

Ich habe mir ein Template für das POD von Module eingerichtet. Dabei habe ich Variablen für den Modulnamen, die Liste der Methoden etc. eingerichtet. So wird mir schon beim Einfügen des Templates etliche Voreinstellungen gemacht...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Antworten mit Zitat

View full thread editor in perl 0.3.n: hurra