Commits

Shlomi Fish committed 51b6210

Refactoring: more generic _write_elem.

Got rid of _handle_elem_of_name_i / _handle_elem_of_name_b .

Comments (0)

Files changed (2)

perl/modules/XML-Grammar-Fiction/lib/XML/Grammar/Fiction/FromProto.pm

 
 use Data::Dumper;
 
-my %lookup = (map { $_ => 1 } qw( li ol ul ));
+my %lookup = (map { $_ => $_ } qw( li ol ul ));
 
-sub _is_passthrough_elem
+around '_calc_passthrough_cb' => sub
 {
-    my ($self, $name) = @_;
+    my $orig = shift;
+    my $self = shift;
+    my ($name) = @_;
 
-    return exists($lookup{$name});
-}
+    if ($lookup{$name})
+    {
+        return $name;
+    }
+
+    return $orig->($self, @_);
+};
 
 sub _output_tag
 {

perl/modules/XML-Grammar-Fiction/lib/XML/Grammar/FictionBase/TagsTree2XML.pm

 
 has "_writer" => ('isa' => "XML::Writer", 'is' => "rw");
 
-sub _is_passthrough_elem
+my %passthrough_elem =
+(
+    b => sub { return shift->_bold_tag_name(); },
+    i => sub { return shift->_italics_tag_name(); },
+);
+
+sub _calc_passthrough_cb
 {
-    return;
+    my ($self, $name) = @_;
+
+    if (exists($passthrough_elem{$name}))
+    {
+        return $passthrough_elem{$name};
+    }
+    else
+    {
+        return undef();
+    }
+}
+
+sub _calc_passthrough_name
+{
+    my ($self, $name, $elem) = @_;
+
+    my $cb = $self->_calc_passthrough_cb($name);
+
+    if (ref($cb) eq 'CODE')
+    {
+        return $cb->($self, $name, $elem,);
+    }
+    else
+    {
+        return $cb;
+    }
 }
 
 sub _write_Element_elem
         );
         return;
     }
-    elsif ($self->_is_passthrough_elem($name))
+    elsif (defined(my $out_name = $self->_calc_passthrough_name($name, $elem)))
     {
-        return
-        $self->_output_tag_with_childs(
+        return $self->_output_tag_with_childs(
             {
-                start => [$name],
+                start => [$out_name],
                 elem => $elem,
             }
         );
     $self->_write_scene({scene => $elem});
 }
 
-sub _handle_elem_of_name_b
-{
-    my ($self, $elem) = @_;
-
-    $self->_output_tag_with_childs(
-        {
-            start => [$self->_bold_tag_name()],
-            elem => $elem,
-        }
-    );
-}
-
 sub _handle_elem_of_name_br
 {
     my ($self, $elem) = @_;
     return;
 }
 
-sub _handle_elem_of_name_i
-{
-    my ($self, $elem) = @_;
-
-    $self->_output_tag_with_childs(
-        {
-            start => [$self->_italics_tag_name],
-            elem => $elem,
-        }
-    );
-
-    return;
-}
-
 sub _convert_while_handling_errors
 {
     my ($self, $args) = @_;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.