[Jifty-commit] r1923 - in wifty/trunk: lib/Wifty lib/Wifty/Form lib/Wifty/Form/Field share/web/templates

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sat Sep 2 22:59:56 EDT 2006


Author: nelhage
Date: Sat Sep  2 22:59:55 2006
New Revision: 1923

Added:
   wifty/trunk/lib/Wifty/Form/
   wifty/trunk/lib/Wifty/Form/Field/
   wifty/trunk/lib/Wifty/Form/Field/WikiPage.pm
Modified:
   wifty/trunk/lib/Wifty/Dispatcher.pm
   wifty/trunk/lib/Wifty/Model/Page.pm
   wifty/trunk/lib/Wifty/Model/Revision.pm
   wifty/trunk/share/web/templates/edit

Log:
Trying out a new idea for a Jifty idiom with Wifty.

Make a Wifty::Form::Field::WikiPage J::W::Form::Field subclass, and
have C<content> fields C<render_as> it. Then, rendering pages by
calling C<form_value> for C<content> field of an appropriate
C<UpdatePage> action. Note that this also makes admin mode look nicer.

Modified: wifty/trunk/lib/Wifty/Dispatcher.pm
==============================================================================
--- wifty/trunk/lib/Wifty/Dispatcher.pm	(original)
+++ wifty/trunk/lib/Wifty/Dispatcher.pm	Sat Sep  2 22:59:55 2006
@@ -39,7 +39,11 @@
     $revision->load_by_cols( page => $page->id, id => $rev ) if ($rev);
     set page => $page;
     set revision => $revision;
-    set viewer => Jifty->web->new_action( class => 'UpdatePage', record => $page );
+    my $viewer = Jifty->web->new_action( class => 'UpdatePage', record => $page );
+    if($rev) {
+        $viewer->argument_value(content => $revision->content);
+    }
+    set viewer => $viewer;
     show("/$page_name");
 };
 

Added: wifty/trunk/lib/Wifty/Form/Field/WikiPage.pm
==============================================================================
--- (empty file)
+++ wifty/trunk/lib/Wifty/Form/Field/WikiPage.pm	Sat Sep  2 22:59:55 2006
@@ -0,0 +1,80 @@
+use warnings;
+use strict;
+
+=head1 NAME
+
+Wifty::Form::Field::WikiPage
+
+=head1 DESCRIPTION
+
+A L<Jifty::Web::Form::Field> subclass that renders itself as a text
+field on update, and wikifies itself on read-only display.
+
+=cut
+
+package Wifty::Form::Field::WikiPage;
+use base qw(Jifty::Web::Form::Field::Textarea);
+
+=head2 render_value
+
+Render a wikified view of this field's content.
+
+=cut
+
+sub render_value {
+    my $self = shift;
+    my $field;
+    my $field = '<span';
+    $field .= qq! class="@{[ $self->classes ]}"> !;
+    $field .= $self->wiki_content;
+    $field .= qq!</span>\n!;
+    Jifty->web->out($field);
+    return '';
+    
+}
+
+
+=head2 wiki_content
+
+Wikify this field's C<current_value>
+
+=cut
+
+
+sub wiki_content {
+    my $self     = shift;
+    my $content  = $self->current_value;
+    my $scrubber = HTML::Scrubber->new();
+
+    $scrubber->default(
+        0,
+        {   '*'   => 0,
+            id    => 1,
+            class => 1,
+            href  => qr{^(?:(?:\w+$)|http:|ftp:|https:|/)}i,
+
+            # Match http, ftp and relative urls
+            face   => 1,
+            size   => 1,
+            target => 1
+        }
+    );
+
+    $scrubber->deny(qw[*]);
+    $scrubber->allow(
+        qw[H1 H2 H3 H4 H5 A STRONG EM CODE PRE B U P BR I HR BR SPAN DIV UL OL LI DL DT DD]);
+    $scrubber->comment(0);
+
+    $content = Text::Markdown::markdown( $content );
+    $content = $scrubber->scrub( $content );
+    return ( $content );
+
+}
+
+=head1 SEE ALSO
+
+L<Text::Markdown>, L<Jifty::Web::Form::Field::Textarea>
+
+=cut
+
+1;

Modified: wifty/trunk/lib/Wifty/Model/Page.pm
==============================================================================
--- wifty/trunk/lib/Wifty/Model/Page.pm	(original)
+++ wifty/trunk/lib/Wifty/Model/Page.pm	Sat Sep  2 22:59:55 2006
@@ -11,7 +11,7 @@
 column content =>
     type is 'text',
     label is 'Content',
-    render_as 'textarea';
+    render_as 'Wifty::Form::Field::WikiPage';
 
 column updated =>
     type is 'timestamp',
@@ -32,43 +32,6 @@
 use HTML::Scrubber;
 
 
-=head2 wiki_content [CONTENT]
-
-Wikify either the content of a scalar passed in as an argument or
-this page's "content" attribute.
-
-=cut
-
-sub wiki_content {
-    my $self     = shift;
-    my $content  = shift || $self->content() || '';
-    my $scrubber = HTML::Scrubber->new();
-
-    $scrubber->default(
-        0,
-        {   '*'   => 0,
-            id    => 1,
-            class => 1,
-            href  => qr{^(?:(?:\w+$)|http:|ftp:|https:|/)}i,
-
-            # Match http, ftp and relative urls
-            face   => 1,
-            size   => 1,
-            target => 1
-        }
-    );
-
-    $scrubber->deny(qw[*]);
-    $scrubber->allow(
-        qw[H1 H2 H3 H4 H5 A STRONG EM CODE PRE B U P BR I HR BR SPAN DIV UL OL LI DL DT DD]);
-    $scrubber->comment(0);
-
-    $content = Text::Markdown::markdown( $content );
-    $content = $scrubber->scrub( $content );
-    return ( $content );
-
-}
-
 sub create {
     my $self = shift;
     my %args = (@_);

Modified: wifty/trunk/lib/Wifty/Model/Revision.pm
==============================================================================
--- wifty/trunk/lib/Wifty/Model/Revision.pm	(original)
+++ wifty/trunk/lib/Wifty/Model/Revision.pm	Sat Sep  2 22:59:55 2006
@@ -3,7 +3,7 @@
 
 column page  => refers_to Wifty::Model::Page;
 
-column content => type is 'text', render_as 'textarea';
+column content => type is 'text', render_as 'Wifty::Form::Field::WikiPage';
 
 column created => type is 'timestamp';
 

Modified: wifty/trunk/share/web/templates/edit
==============================================================================
--- wifty/trunk/share/web/templates/edit	(original)
+++ wifty/trunk/share/web/templates/edit	Sat Sep  2 22:59:55 2006
@@ -18,7 +18,7 @@
   copy the source of this page. </p>
 % }
 <% Jifty->web->form->next_page( url => '/view/'.$page->name) %>
-<% $viewer->form_field('content', ($revision->id ? (default_value => $revision->content) : (undef, undef)), rows => 30 )%>
+<% $viewer->form_field('content', rows => 30 )%>
 </div>
 % if($can_edit) {
 <div class="line">


More information about the Jifty-commit mailing list