Debian Codegraph @

example/create-sample-html-document.pl

raw
1 #!/usr/bin/perl
2
3 =head1 ABOUT
4
5 This is a sample program to generate an HTML document using XML::LibXML's
6 DOM routines. It was written to resolve
7 L<https://rt.cpan.org/Ticket/Display.html?id=117923> . Thanks to Dan Jacobson.
8
9 =cut
10
11 use strict;
12 use warnings;
13
14 use XML::LibXML;
15
16 {
17 my $doc = XML::LibXML->createDocument;
18
19 # A small Domain-Specific-Language for generating DOM:
20 my $_text = sub {
21 my ($content) = @_;
22
23 return $doc->createTextNode($content);
24 };
25
26 # Short for element.
27 my $_el = sub {
28 my $name = shift;
29 my $param = shift;
30 my $attrs = {};
31 if (ref($param) eq 'HASH')
32 {
33 $attrs = $param;
34 $param = shift;
35 }
36 my $childs = $param;
37
38 my $elem = $doc->createElementNS("", $name);
39
40 while (my ($k, $v) = each %$attrs)
41 {
42 $elem->setAttribute($k, $v);
43 }
44
45 foreach my $child (@$childs)
46 {
47 $elem->appendChild($child);
48 }
49
50 return $elem;
51 };
52
53 my $html = $_el->(
54 'html',
55 [
56 $_el->(
57 'head',
58 [
59 $_el->(
60 'title',
61 [
62 $_text->("Sample HTML document as generated by XML::LibXML"),
63 ],
64 ),
65 $_el->(
66 'meta',
67 { 'http-equiv' => 'Content-Type',
68 'content' => 'text/html; charset=utf-8'
69 },
70 [],
71 ),
72 ],
73 ),
74 $_el->(
75 'body',
76 [
77 $_el->(
78 'p',
79 [
80 $_text->("Introducing a link - "),
81 $_el->(
82 'a',
83 { 'href' => 'http://www.wikipedia.org/', },
84 [
85 $_text->("Link to Wikipedia"),
86 ],
87 ),
88 $_text->(". We hope you enjoyed it."),
89 ],
90 ),
91 $_el->(
92 'p',
93 [
94 $_el->(
95 'img',
96 { 'src' => 'http://example.com/non-exist.png',
97 'alt' => 'non-existing image',
98 },
99 [],
100 ),
101 ],
102 ),
103 $_el->(
104 'ol',
105 [
106 $_el->(
107 'li',
108 [
109 $_el->(
110 'p',
111 [
112 $_text->("First item."),
113 ],
114 ),
115 ],
116 ),
117 $_el->(
118 'li',
119 [
120 $_el->(
121 'p',
122 [
123 $_text->("Second item."),
124 ],
125 ),
126 ],
127 ),
128 ],
129 ),
130 ],
131 ),
132 ],
133 );
134 $doc->setDocumentElement( $html );
135
136 print $doc->toStringHTML();
137 }
138
139 =head1 COPYRIGHT & LICENSE
140
141 Copyright 2016 by Shlomi Fish
142
143 This program is distributed under the MIT (X11) License:
144 L<http://www.opensource.org/licenses/mit-license.php>
145
146 Permission is hereby granted, free of charge, to any person
147 obtaining a copy of this software and associated documentation
148 files (the "Software"), to deal in the Software without
149 restriction, including without limitation the rights to use,
150 copy, modify, merge, publish, distribute, sublicense, and/or sell
151 copies of the Software, and to permit persons to whom the
152 Software is furnished to do so, subject to the following
153 conditions:
154
155 The above copyright notice and this permission notice shall be
156 included in all copies or substantial portions of the Software.
157
158 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
159 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
160 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
161 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
162 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
163 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
164 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
165 OTHER DEALINGS IN THE SOFTWARE.
166
167 =cut