Commits

Shlomi Fish committed 7e5fcb5

Comments (0)

Files changed (2)

lib/Shlomif/Homepage/NavData/JSON.pm

 
 use MyNavData;
 
-use JSON qw(encode_json);
-use YAML::Syck ();
+use HTML::Widgets::NavMenu::ToJSON;
+use HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML;
 
 sub output_fully_expanded_as_json
 {
             'JSON_Data_Persistence.yml',
         );
 
-    my ($id_persistence) = YAML::Syck::LoadFile($id_persistence_filename);
+    my $persistence =
+        HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML->new(
+            {
+                filename => $id_persistence_filename,
+            }
+        );
 
-    $id_persistence ||= {
-        id_persistence => { next_id => 1, paths_ids => { }, },
-    };
-
-    my $ptr = $id_persistence->{id_persistence};
-
-    my $get_id_from_cache = sub
-    {
-        my $url = shift;
-        if (! exists($ptr->{paths_ids}->{$url}))
+    return HTML::Widgets::NavMenu::ToJSON->new(
         {
-            $ptr->{paths_ids}->{$url} = ($ptr->{next_id}++);
+            data_persistence_store => $persistence,
+            tree_contents => $params{tree_contents},
         }
-        return $ptr->{paths_ids}->{$url};
-    };
-
-    my $process_sub_tree;
-
-    $process_sub_tree = sub
-    {
-        my ($sub_tree) = @_;
-
-        my @keys = (grep { $_ ne 'subs' } keys %{$sub_tree});
-
-        my $has_subs = exists($sub_tree->{subs});
-
-        return
-        {
-            (exists($sub_tree->{url})
-                ? (id => $get_id_from_cache->($sub_tree->{url}))
-                : ()
-            ),
-            (map { $_ => $sub_tree->{$_} } @keys),
-            $has_subs
-            ?  (subs => [ map { $process_sub_tree->($_) }
-                    grep { ! exists($_->{separator}) }
-                    @{$sub_tree->{subs}}
-                ])
-            : (),
-        };
-    };
-
-    my $ret = encode_json(
-        $process_sub_tree->($params{tree_contents})->{'subs'}
-    );
-
-    YAML::Syck::DumpFile(
-        $id_persistence_filename, $id_persistence,
-    );
-
-    return $ret;
+    )->output_as_json();
 }
 
 1;

lib/Shlomif/Homepage/NavData/JSON_Data_Persistence.yml

---- 
-id_persistence: 
+---
+id_persistence:
   next_id: 321
-  paths_ids: 
-    "": 1
+  paths_ids:
+    '': 1
     DeCSS/: 288
     MathVentures/: 99
     MathVentures/3d-outof-4d-mathml.xhtml: 100