/[suikacvs]/markup/h2h/implementation/classic/generate-atom-entry.pl
Suika

Contents of /markup/h2h/implementation/classic/generate-atom-entry.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Mon Jan 8 02:10:01 2007 UTC (17 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +1 -1 lines
File MIME type: text/plain
*** empty log message ***

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3     use utf8;
4    
5     use lib q</home/wakaba/work/manakai/lib>;
6    
7     our $REPOSITORY_PATH = q</home/wakaba/public_html/d/>;
8     our $FEED_TAG = q<urn:x-suika-fam-cx:fuyubi:>;
9     our $BASE_URI = q<http://suika.fam.cx/~wakaba/d/>;
10     our $BASE_LANG = 'ja';
11     our $AUTHOR_NAME = q<わかば>;
12     our $AUTHOR_URI = q<http://suika.fam.cx/~wakaba/who?>;
13     our $AUTHOR_MAIL = q<w@suika.fam.cx>;
14    
15     use Message::Markup::Atom;
16     use Message::Markup::H2H;
17     use Message::DOM::DOMFeature;
18     use Message::DOM::GenericLS;
19     use Message::DOM::SimpleLS;
20     use Encode::EUCJP1997;
21    
22     my ($year, $month, $day) = @ARGV;
23     $year += 0;
24     $month += 0;
25     $day += 0;
26    
27     my $atom = q<http://www.w3.org/2005/Atom>;
28     my $fe = q<http://suika.fam.cx/www/2006/feature/>;
29     my $cfg = q<http://suika.fam.cx/www/2006/dom-config/>;
30     my $html = q<http://www.w3.org/1999/xhtml>;
31     my $xml = q<http://www.w3.org/XML/1998/namespace>;
32     my $xhtml2 = q<http://www.w3.org/2002/06/xhtml2/>;
33     my $h2h = q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Markup/H2H/>;
34     my $xmlns = q<http://www.w3.org/2000/xmlns/>;
35    
36 wakaba 1.2 my $gls = $Message::DOM::ImplementationRegistry->get_dom_implementation
37 wakaba 1.1 ({
38     $fe.'GenericLS' => '3.0',
39     });
40     my $aimpl = $gls->get_feature ($fe.'Atom' => '1.0');
41    
42     my $base_file_name = $REPOSITORY_PATH.sprintf ('%04d/d%04d%02d%02d',
43     $year, $year, $month, $day);
44    
45     my $h2h_data = '';
46     if (-f $base_file_name.'.hnf') {
47     local $/ = undef;
48     open my $h2h_file, '<', $base_file_name.'.hnf'
49     or die "$0: $base_file_name.hnf: $!";
50     $h2h_data = Encode::decode ('euc-jp-1997', <$h2h_file>);
51     close $h2h_file;
52     }
53    
54     my $h2h_p = $gls->create_gls_parser ({
55     $fe.'H2H' => '1.0',
56     });
57     my $h2h_doc = $h2h_p->parse_string ($h2h_data);
58    
59     my $i = 0;
60    
61     for my $section (@{$h2h_doc->get_elements_by_tag_name_ns ($html, 'body')
62     ->[0]->child_nodes}) {
63     next if $section->node_type != $section->ELEMENT_NODE;
64     next if $section->local_name != 'section';
65     next if $section->namespace_uri != $html;
66    
67     $i++;
68    
69     my $atom_doc = $aimpl->create_atom_entry_document
70     ($FEED_TAG.$year.':'.$month.':'.$day.':'.$i);
71     $atom_doc->dom_config->set_parameter ($cfg.'create-child-element' => 1);
72    
73     my $atom_entry = $atom_doc->document_element;
74     $atom_entry->set_attribute_ns ($xml, 'xml:base', $BASE_URI);
75     $atom_entry->set_attribute_ns ($xml, 'xml:lang', $BASE_LANG);
76     $atom_entry->set_attribute_ns ($xmlns, xmlns => $html);
77    
78     my $atom_content = $atom_entry->content_element;
79     $atom_content->type ('xhtml');
80    
81     my $atom_container = $atom_content->container;
82    
83     my $hour = 0;
84     my $minute = 0;
85     my $tz = '-00:00';
86     my @section_children = @{$section->child_nodes};
87     for my $el (@section_children) {
88     next if $el->node_type != $el->ELEMENT_NODE;
89    
90     my $xuri = $el->manakai_expanded_uri;
91    
92     if ($xuri eq $xhtml2.'h') {
93     my $atom_title = $atom_entry->title_element;
94     $atom_title->type ('xhtml');
95     my $atom_title_container = $atom_title->container;
96     my @el_children = @{$el->child_nodes};
97     for (@el_children) {
98     $atom_title_container->append_child ($atom_doc->adopt_node ($_));
99     }
100    
101     if ($atom_title_container->text_content
102     =~ /\(\@(\d\d):(\d\d) ([+-]\d\d:\d\d)\)\s*$/) {
103     $hour = $1 + 0;
104     $minute = $2 + 0;
105     $tz = $3;
106     }
107     } elsif ($xuri eq $h2h.'cat') {
108     ## TODO:
109     } else {
110     $atom_container->append_child ($atom_doc->adopt_node ($el));
111     }
112     }
113    
114     $atom_entry->published_element->text_content
115     (sprintf '%04d-%02d-%02dT%02d:%02d:00%s',
116     $year, $month, $day, $hour, $minute, $tz);
117    
118     for my $author_el ($atom_entry->append_child
119     ($atom_doc->create_element_ns ($atom, 'author'))) {
120     $author_el->name ($AUTHOR_NAME);
121     $author_el->uri ($AUTHOR_URI) if defined $AUTHOR_URI;
122     $author_el->email ($AUTHOR_MAIL) if defined $AUTHOR_MAIL;
123     }
124    
125     for my $link_el ($atom_entry->append_child
126     ($atom_doc->create_element_ns ($atom, 'link'))) {
127     $link_el->rel ('alternate');
128     $link_el->href (sprintf 'd%04d%02d.%s.html#d%d-%d',
129     $year, $month, $BASE_LANG, $day, $i);
130     $link_el->type ('text/html');
131     $link_el->hreflang ($BASE_LANG);
132     }
133    
134     for my $link_el ($atom_entry->append_child
135     ($atom_doc->create_element_ns ($atom, 'link'))) {
136     $link_el->rel ('self');
137     $link_el->href (sprintf '%04d/d%04d%02d%02d-%d.%s.atom',
138     $year, $year, $month, $day, $i, $BASE_LANG);
139     $link_el->type ('application/atom+xml');
140     $link_el->hreflang ($BASE_LANG);
141     }
142    
143     my $entry_file_name = $base_file_name . '-' . $i . '.'.$BASE_LANG.'.atom';
144     my $ls = $gls->create_gls_serializer ({
145     $fe.'SerializeDocumentInstance' => '1.0',
146     });
147    
148     open my $entry_file, '>', $entry_file_name
149     or die "$0: $entry_file_name: $!";
150    
151     warn qq<Write to "$entry_file_name"\n>;
152     print $entry_file Encode::encode ('utf-8', $ls->write_to_string ($atom_doc));
153     close $entry_file;
154     system 'chmod', 'go+r', $entry_file_name;
155     $? == -1 and die "$0: chmod $entry_file_name: $!";
156     }
157    
158    

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24