1 # =====================================================================
2 # bloggerSetTemplate.awk: W-TW BloggerAPI 1.0 template builder.
3 #
4 # Copyright (c) 2009,2010,2011,2012,2021 Carlo Strozzi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 dated June, 1991.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # =====================================================================
20 #
21 # This program is an attempt to provide a template system that can be
22 # easily customized by a blog editor with only minimal effort. The set
23 # of tags that can be inserted in the template resembles those used in
24 # the original Blogger 1.0, so that the many "skins" already available
25 # for that system can be easily adapted to work also with TW.
26 #
27 # See http://archives.blogspot.com/2002_03_01_archive.html for info.
28 #
29 # =====================================================================
30
31 BEGIN {
32
33 header = footer = tpl = tmp = tmp1 = ""; delete a
34
35 if ((tv_xml=_rcget("tw_stem",1) "/tv.xml") !~ /^\/[^\/]+\/[^\/]+/)
36 tv_xml = "/dev/null"
37 }
38
39 NR == 1 { tpl = $0 }
40 NR >= 2 { tpl = tpl "\n" $0 }
41
42 END {
43
44 gsub(/\$\[/,"\\$[",tpl) # neutralize any CSA tags first!
45
46 header = footer = tpl
47
48 # Extract any user-defined name/value pairs.
49 split(tpl,a,/.*/,">",a[tmp])
54 print " tv_xml
55 }
56
57 # In case this def is missing make sure the header is cleared,
58 # or the whole template will be used as header!.
59 if (!sub(/.*/,_NULL,header)) header = _NULL
60 sub(/<\/BlogPostHeader>.*/,_NULL,header)
61 gsub(//,_NULL,header) # just in case
62
63 # Both BlogPostDate and BlogPostTime must be present in header,
64 # in any order and possibly enclosed in HTML comments "".
65 # If either tag is missing then neither will be replaced. Note
66 # that page expiration date and time are not supported here yet.
67
68 tmp = _NULL
69
70 if (header ~ /<\$BlogPostDate\$>.*<\$BlogPostTime\$>/)
71 tmp = ""
72 else if (header ~ /<\$BlogPostTime\$>.*<\$BlogPostDate\$>/)
73 tmp = ""
74 if (tmp != _NULL) {
75 # Multiple date/date pairs are supported, provided they
76 # are enclosed in appropriate $[tpl.if...] conditionals.
77 gsub(/<\$BlogPostDate\$>/,"",header)
78 gsub(/<\$BlogPostTime\$>/,"",header)
79 header = tmp "" header
80 }
81
82 # In case this def is missing make sure the footer is cleared,
83 # or the whole template will be used as footer!.
84 if (!sub(/.*/,_NULL,footer)) footer = _NULL
85 sub(/<\/BlogPostFooter>.*/,_NULL,footer)
86
87 gsub(//,_NULL,footer) # just in case
88
89 # Both BlogPostAuthor and BlogPostURL must be present in footer,
90 # in any order and possibly enclosed in HTML comments "".
91 # If either tag is missing then neither will be replaced.
92 # Like in the main tamplate body, <$BlogURL$>/<$BlogPostName$>
93 # is treated as a synonym of <$BlogPostURL$> also in the footer
94 # section.
95
96 #sub(/<\$BlogURL\$>\/<\$BlogPostName\$>/,"<$BlogPostURL$>",footer)
97
98 if (footer ~ /<\$BlogPostAuthor\$>.*<\$Blog(Post|Item)URL\$>/)
99 tmp = ""
100 else if (footer ~ /<\$Blog(Post|Item)URL\$>.*<\$BlogPostAuthor\$>/)
101 tmp = ""
102 else tmp = _NULL
103
104 if (tmp != _NULL) {
105 # Multiple author/permalink pairs are supported, provided they
106 # are enclosed in appropriate $[tpl.if...] conditionals.
107 gsub(/<\$BlogPostAuthor\$>/,"",footer)
108 gsub(/<\$BlogURL\$>\/<\$Blog(Post|Item)URL\$>/,"",footer)
109 footer = tmp footer
110 }
111
112 # Perform all other replacements in main template. Substitutions
113 # with $[...] CSA tags will only work in the main template and in
114 # sub-templates included by the former, but they are ignored in
115 # "*.fmt" files.
116
117 # Order-independend replacements in (only done in main template, see above).
118
119 gsub(/<\$BlogUploadURL\$>/,"$[CSA_URL:x]/$[CSA_LANG:x]/$[TNS_ATTACH_PUBDIR:x]/$[tpl.var.tw.group.unx]",tpl)
120
121 gsub(/<\$BlogTitle\$>/,"$[tpl.var.html.title:x]",tpl)
122 gsub(/<\$BlogDescription\$>/,"$[tpl.var.tw.descr:x]",tpl)
123 gsub(/<\$BlogPostBody\$>/,"$[tpl.include.html.body:c]",tpl)
124 gsub(/<\$BlogPostName\$>/,"$[tpl.var.tw.page:x]",tpl)
125 gsub(/<\$BlogPostURL\$>/,"$[tpl.var.tw.page:Px]",tpl)
126 gsub(/<\$BlogCalendar\$>/,"$[tpl.include.tw.group.cal:c]",tpl)
127 gsub(/<\$BlogCatCloud\$>/,"$[tpl.include.tw.group.cat:c]",tpl)
128 gsub(/<\$BlogTagCloud\$>/,"$[tpl.include.tw.group.tag:c]",tpl)
129 gsub(/<\$BlogRecentPosts\$>/,"$[tpl.include.tw.recent.links:c]",tpl)
130 gsub(/<\$BlogRecentComments\$>/,"$[tpl.include.tw.recent.cmts:c]",tpl)
131 gsub(/<\$BlogArchives\$>/,"$[tpl.include.tw.year.links:c]",tpl)
132 gsub(/<\$BlogUserNick\$>/,"$[CSA_AUTH_USER:x]",tpl)
133 gsub(/<\$BlogUser\$>/,"$[TNS_AUTH_DISPLAY:x]",tpl)
134 gsub(/<\$BlogCurrentDate\$>/,"$[tpl.var.tw.date:x]",tpl)
135 gsub(/<\$BlogSessionBox\$>/,"$[tpl.include.nav.session:c]",tpl)
136 gsub(/<\$BlogPageID\$>/,"$[tpl.var.tw.node]",tpl)
137
138 # This is currently not done, but it may be enabled in the future.
139 #gsub(/<\$BlogPostCommentCount\$>/,"$[tpl.var.tw.page.cmt.count:x]",tpl)
140
141 gsub(/<\$BlogBaseHref\$>/,"",tpl)
142
143 gsub(/<\$BlogMetaData\$>/,"$[tpl.include.tw.page.meta:c]$[tpl.var.tw.meta]",tpl)
144
145 gsub(/<\$BlogAvatarURL\$>/,"$[TNS_GRAVATAR_URL:x]/$[tpl.var.tw.user.email.md5:x]?r=$[TNS_GRAVATAR_RATING:x]",tpl)
146
147 sub(/.*<\/BlogPostHeader>/,_NULL,tpl)
148 sub(/.*<\/BlogPostFooter>/,_NULL,tpl)
149
150 # Replace conditionals. They apply also to header and footer sections,
151 # as well as embedded template specs, and must come *after* the above
152 # substitutions.
153
154 delete a; a[1] = tpl; a[2] = header; a[3] = footer
155
156 for (tmp in a) {
157 gsub(//,"$[tpl.if.tw.ispage]",a[tmp])
158 gsub(/<\/BlogIfPost>/,"$[tpl.fi.tw.ispage]",a[tmp])
159 gsub(//,"$[tpl.if.tw.isview]",a[tmp])
160 gsub(/<\/BlogIfNotPost>/,"$[tpl.fi.tw.isview]",a[tmp])
161 gsub(//,"$[tpl.if.tw.editor]",a[tmp])
162 gsub(/<\/BlogIfEditor>/,"$[tpl.fi.tw.editor]",a[tmp])
163 gsub(//,"$[tpl.if.tw.global.editor]",a[tmp])
164 gsub(/<\/BlogIfAdmin>/,"$[tpl.fi.tw.global.editor]",a[tmp])
165 gsub(//,"$[tpl.if.tw.public]",a[tmp])
166 gsub(/<\/BlogIfReader>/,"$[tpl.fi.tw.public]",a[tmp])
167 gsub(//,"$[tpl.if.tw.user]",a[tmp])
168 gsub(/<\/BlogIfUser>/,"$[tpl.fi.tw.user]",a[tmp])
169 }
170
171 tpl = a[1]; header = a[2]; footer = a[3]
172
173 # with the relevant inclusion directives.
174 #
175 #sub(/.*<\/BlogPostHeader>/,\
176 # "$[tpl.include.tw.page.header:c]",tpl)
177 #
178 #sub(/.*<\/BlogPostFooter>/,\
179 # "$[tpl.include.tw.page.footer:c]",tpl)
180
181 # Only at this stage we can finally create header and footer fmt files.
182 printf("\n%s\n\n",header) > ENVIRON["tw_stem"] "/tw-page-header.fmt"
183
184 printf("\n%s\n\n",footer) > ENVIRON["tw_stem"] "/tw-page-footer.fmt"
185
186 # Replace header and footer sections in main template
187 # Now look for template specs inside main tamplate.
188 tmp = tpl
189 if (sub(/.*/,_NULL,tmp) && \
190 sub(/<\/BlogTemplates>.*/,_NULL,tmp) && \
191 split(tmp,a,/[ \t\n\r]*/,": ",a[tmp])
194 sub(/[ \t\n\r]*<\/tw:tpl>.*/,_NULL,a[tmp])
195 if (a[tmp] ~ /^tw-[-a-z0-9]+\.(fmt|txt) *:/) {
196 tmp1 = a[tmp]
197 sub(/ *:.*/,_NULL,tmp1)
198 sub(/[^:]+:[ \r\n]*/,_NULL,a[tmp])
199 # Page header/footer are already handled separately with
200 # the relevant <...> tags.
201 if (tmp1 ~ /^tw-page-(header|footer)\.fmt$/) continue
202 printf("\n%s\n\n",tmp1,a[tmp],tmp1) > ENVIRON["tw_stem"] "/" tmp1
203 close(tmp1)
204 }
205 }
206 }
207
208 # Strip all template specs from remaining code.
209 sub(/.*<\/BlogTemplates>/,_NULL,tpl)
210
211 # Order-dependend replacements.
212
213 sub(/<\$BlogPingbackLink\$>/,"$[tpl.if.tw.ispage]$[tpl.fi.tw.ispage]",tpl)
214
215 #gsub(/<\$BlogPostURL\$>/,"<$BlogURL$>/<$BlogPostName$>",tpl)
216
217 gsub(/<\$BlogURL\$>/,\
218 "$[CSA_RPC_URI:x]/$[CSA_LANG:x]/$[tpl.var.tw.group:Px]",tpl)
219
220 #gsub(/<\$BlogPostName\$>/,"$[tpl.var.tw.page:Px]",tpl)
221
222 printf("%s", tpl)
223 }
224
225 # EOF