/[Perly]/lib/Perly/View.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/Perly/View.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations)
Sun Jun 1 17:31:43 2008 UTC (15 years, 11 months ago) by dpavlin
File size: 2881 byte(s)
added Perly::SyntaxHighlight which generates sytax highlight of perl code
using Syntax::Highlight::Perl which is similar enough to CodePress so that
code in html doesn't look ugly
1 package Perly::View;
2
3 use strict;
4 use warnings;
5
6 use Jifty::View::Declare -base;
7
8 use Perly::SyntaxHighlight;
9 use Data::Dump qw/dump/;
10
11 template '/' => page {
12 h1 { _("Available code") }
13 div { show 'code_list' }
14 };
15
16 template '/edit' => page {
17 h1 { _("Enter your code") }
18 div { show 'code_editor' }
19 };
20
21 private template 'code_list' => sub {
22 ul {
23 my $coll = Perly::Model::CodeCollection->new();
24 $coll->unlimit;
25 while ( my $code = $coll->next ) {
26 li {
27 a {
28 attr { href => '/edit/id=' . $code->id, },
29 $code->name,
30 },
31 small { length( $code->source ), ' bytes ', $code->created_on },
32 a {
33 attr { href => '/run/code=' . $code->id, }, 'run'
34 },
35 outs_raw( Perly::SyntaxHighlight->color( $code->source ) )
36 }
37 }
38 }
39 };
40
41 sub codepress {
42 my $codepress = 1;
43 $codepress = current_user->user_object->codepress if ( current_user->id );
44 return { render_as => 'Textarea' } unless ( $codepress );
45 return { render_as => 'Jifty::Plugin::CodePress::Textarea' };
46 }
47
48 private template 'code_editor' => sub {
49 my $op = 'Create';
50 my $code = Perly::Model::Code->new();
51 if ( my $id = get 'id' ) {
52 $code->load( $id ) or die "can't load $id";
53 warn "loaded ", length( $code->source ), " bytes";
54 $op = 'Update';
55 }
56
57 warn "## editor: ", dump( codepress );
58
59 form {
60 my $action = new_action( class => $op . 'Code', arguments => { $code ? $code->as_hash : undef } );
61 render_param( $action => 'name' );
62 render_param( $action => 'source', cols => 80, rows => 25, language => 'perl', render_as => codepress->{render_as} );
63 #render_action( $action => [ 'source' => { cols => 80, rows => 30 }, ] );
64 form_submit( label => _($op) );
65 # IE problem http://support.microsoft.com/kb/281197
66 form_next_page( url => '/run/code=' . $action->argument_value('id') );
67 }
68 };
69
70 template '/upload' => page {
71 h1 { _("Upload new file") },
72 form {
73 my $action = new_action( class => 'Upload' );
74 render_action( $action => [ 'filename', 'content', 'file', 'format' ] );
75 form_submit(
76 label => _('Upload'),
77 );
78 }
79 };
80
81 template '/run' => page {
82 my $action = new_action( class => 'Run' );
83 h1 { _("Run code on some input") },
84 form {
85 render_param( $action => 'input', default_value => get('input') ),
86 render_param( $action => 'code', default_value => get('code') ),
87 form_submit(
88 label => _('Run'),
89 );
90 };
91
92 #warn dump( $action->result->content );
93
94 if ( my $input = $action->result->content( 'input' ) ) {
95 h2 { _("Input data") },
96 pre { $input }
97 }
98
99 if ( my $output = $action->result->content( 'output' ) ) {
100 h2 { _("Output result") },
101 pre { $output }
102 }
103 };
104
105 template '/prefs' => page {
106 my $action = new_action( class => 'UpdateUser', arguments => { id => current_user->id } );
107 h1 { _("My preferences") },
108 form {
109 render_action( $action => [ 'name', 'email', 'codepress' ] );
110 form_submit(
111 label => _('Save changes'),
112 );
113 };
114 };
115
116 1;

  ViewVC Help
Powered by ViewVC 1.1.26