1 |
Douglas Thrift |
126 |
// Site Mapper |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "SiteMapper.hpp" |
8 |
|
|
#include "Matcher.hpp" |
9 |
Douglas Thrift |
128 |
#include "Page.hpp" |
10 |
Douglas Thrift |
126 |
|
11 |
Douglas Thrift |
128 |
string program; |
12 |
Douglas Thrift |
152 |
bool debug(false); |
13 |
Douglas Thrift |
128 |
|
14 |
Douglas Thrift |
126 |
int main(int argc, char* argv[]) |
15 |
|
|
{ |
16 |
Douglas Thrift |
128 |
program = argv[0]; |
17 |
Douglas Thrift |
132 |
|
18 |
Douglas Thrift |
126 |
string siteIndex, siteMap; |
19 |
|
|
|
20 |
Douglas Thrift |
152 |
for (int index(1); index < argc; index++) |
21 |
Douglas Thrift |
126 |
{ |
22 |
Douglas Thrift |
128 |
string arg(argv[index]); |
23 |
|
|
Matcher matcher; |
24 |
Douglas Thrift |
126 |
|
25 |
Douglas Thrift |
128 |
if (arg == matcher("^-index=(.*)$")) |
26 |
|
|
{ |
27 |
|
|
siteIndex = matcher[1]; |
28 |
|
|
} |
29 |
|
|
else if (arg == matcher("^-map=(.*)$")) |
30 |
|
|
{ |
31 |
|
|
siteMap = matcher[1]; |
32 |
|
|
} |
33 |
Douglas Thrift |
133 |
else if (arg == "-D") |
34 |
|
|
{ |
35 |
|
|
if (!debug) debug = true; |
36 |
|
|
} |
37 |
Douglas Thrift |
126 |
} |
38 |
|
|
|
39 |
Douglas Thrift |
156 |
if (!siteIndex.empty() && !siteMap.empty()) |
40 |
Douglas Thrift |
132 |
{ |
41 |
Douglas Thrift |
128 |
SiteMapper mapper(siteIndex, siteMap); |
42 |
|
|
} |
43 |
|
|
else |
44 |
|
|
{ |
45 |
Douglas Thrift |
133 |
cout << "Usage: " << program << " -index=index -map=map [-D]\n"; |
46 |
Douglas Thrift |
128 |
} |
47 |
Douglas Thrift |
129 |
|
48 |
Douglas Thrift |
126 |
return 0; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap) |
52 |
|
|
{ |
53 |
Douglas Thrift |
133 |
oldMap(siteMap); |
54 |
Douglas Thrift |
142 |
newIndex(siteIndex); |
55 |
Douglas Thrift |
134 |
newMap(siteMap); |
56 |
Douglas Thrift |
133 |
} |
57 |
|
|
|
58 |
|
|
void SiteMapper::oldMap(const string& siteMap) |
59 |
|
|
{ |
60 |
Douglas Thrift |
154 |
ext::Handle<xml::Document> document(xml::Parse(siteMap)); |
61 |
|
|
ext::Handle<xml::Node> list(*document/"page"/"section"/"list"); |
62 |
Douglas Thrift |
133 |
|
63 |
Douglas Thrift |
237 |
comment = ext::String(*document/"comment()"); |
64 |
Douglas Thrift |
133 |
|
65 |
Douglas Thrift |
137 |
oldMap(pages, list); |
66 |
Douglas Thrift |
133 |
} |
67 |
|
|
|
68 |
Douglas Thrift |
154 |
void SiteMapper::oldMap(vector<Page>& pages, xml::Node* list) |
69 |
Douglas Thrift |
135 |
{ |
70 |
Douglas Thrift |
154 |
xml::NodeSet nodes(*list/"item"); |
71 |
Douglas Thrift |
135 |
|
72 |
Douglas Thrift |
154 |
for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End(); |
73 |
|
|
++node) |
74 |
Douglas Thrift |
135 |
{ |
75 |
Douglas Thrift |
237 |
string url(ext::String(**node/"link"/"@address")), |
76 |
|
|
title(ext::String(**node/"link")); |
77 |
Douglas Thrift |
154 |
Page page(url, title); |
78 |
|
|
ext::Handle<xml::Node> list(**node/"list"); |
79 |
Douglas Thrift |
141 |
|
80 |
Douglas Thrift |
154 |
if (!list.IsEmpty()) oldMap(page.getChildren(), list); |
81 |
Douglas Thrift |
135 |
|
82 |
|
|
pages.push_back(page); |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
|
86 |
Douglas Thrift |
142 |
void SiteMapper::newIndex(const string& siteIndex) |
87 |
Douglas Thrift |
133 |
{ |
88 |
Douglas Thrift |
154 |
ext::Handle<xml::Document> document(xml::Parse(siteIndex)); |
89 |
|
|
xml::NodeSet nodes(*document/"index"/"page"); |
90 |
Douglas Thrift |
138 |
|
91 |
Douglas Thrift |
154 |
for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End(); |
92 |
|
|
++node) |
93 |
Douglas Thrift |
138 |
{ |
94 |
Douglas Thrift |
237 |
string address(ext::String(**node/"address")); |
95 |
|
|
string port(ext::String(**node/"port")); |
96 |
Douglas Thrift |
139 |
|
97 |
Douglas Thrift |
154 |
if (!port.empty()) |
98 |
Douglas Thrift |
139 |
{ |
99 |
Douglas Thrift |
154 |
address += ':' + port; |
100 |
Douglas Thrift |
139 |
} |
101 |
|
|
|
102 |
Douglas Thrift |
237 |
string path(ext::String(**node/"path")), |
103 |
|
|
title(ext::String(**node/"title")); |
104 |
Douglas Thrift |
154 |
Page page(address, path, title); |
105 |
Douglas Thrift |
142 |
Matcher matcher; |
106 |
Douglas Thrift |
140 |
|
107 |
Douglas Thrift |
142 |
if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou") |
108 |
|
|
+ "glas\\sThrift's\\sBlog:\\s(.+)$")) |
109 |
Douglas Thrift |
140 |
{ |
110 |
Douglas Thrift |
156 |
if (Matcher("^\\w+\\s\\d{4}\\sArchives$") == matcher[1]) |
111 |
Douglas Thrift |
142 |
{ |
112 |
|
|
page.setTitle(matcher[1]); |
113 |
|
|
|
114 |
|
|
if (newIndex(pages, page)) continue; |
115 |
|
|
} |
116 |
|
|
else continue; |
117 |
|
|
} |
118 |
|
|
else if (page == matcher("^Douglas\\sThrift's.+Website\\s\\|\\s(.+)$")) |
119 |
|
|
{ |
120 |
Douglas Thrift |
140 |
page.setTitle(matcher[1]); |
121 |
|
|
|
122 |
Douglas Thrift |
142 |
if (newIndex(pages, page)) continue; |
123 |
Douglas Thrift |
140 |
} |
124 |
Douglas Thrift |
142 |
else continue; |
125 |
|
|
|
126 |
Douglas Thrift |
143 |
multimap<string, Page> items; |
127 |
|
|
|
128 |
|
|
newPages.insert(pair<string, multimap<string, Page> |
129 |
|
|
>(page.getAddress(), items)).first->second.insert(pair<string, |
130 |
|
|
Page>(page.getChildOf(), page)); |
131 |
Douglas Thrift |
138 |
} |
132 |
Douglas Thrift |
126 |
} |
133 |
Douglas Thrift |
133 |
|
134 |
Douglas Thrift |
142 |
bool SiteMapper::newIndex(vector<Page>& pages, Page& page) |
135 |
|
|
{ |
136 |
Douglas Thrift |
153 |
for (unsigned index(0); index < pages.size(); ++index) |
137 |
Douglas Thrift |
142 |
{ |
138 |
|
|
if (pages[index] == page.getAddress()) |
139 |
|
|
{ |
140 |
|
|
Matcher matcher; |
141 |
|
|
|
142 |
|
|
if (pages[index] == page) |
143 |
|
|
{ |
144 |
|
|
page.setChildren(pages[index].getChildren()); |
145 |
|
|
|
146 |
|
|
pages[index] = page; |
147 |
|
|
|
148 |
Douglas Thrift |
156 |
cout << "Updated: " << page.getUrl() << '\n'; |
149 |
|
|
|
150 |
Douglas Thrift |
142 |
return true; |
151 |
|
|
} |
152 |
|
|
else if (matcher('^' + pages[index].getPath()) == page) |
153 |
|
|
{ |
154 |
|
|
page.setChildOf(matcher[0]); |
155 |
|
|
|
156 |
|
|
if (matcher('^' + pages[index].getTitle() + "\\s\\|\\s(.+)$") |
157 |
|
|
== page) |
158 |
|
|
{ |
159 |
|
|
page.setTitle(matcher[1]); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
return newIndex(pages[index].getChildren(), page); |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
return false; |
168 |
|
|
} |
169 |
|
|
|
170 |
Douglas Thrift |
133 |
void SiteMapper::newMap(const string& siteMap) |
171 |
|
|
{ |
172 |
Douglas Thrift |
144 |
ofstream fout(siteMap.c_str()); |
173 |
|
|
|
174 |
|
|
fout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
175 |
|
|
<< "<?xml-stylesheet type=\"text/xsl\" href=\"stylesheets/sitemap.xsl" |
176 |
|
|
<< "\"?>\n" |
177 |
|
|
<< "<!DOCTYPE page SYSTEM \"stylesheets/page.dtd\">\n" |
178 |
Douglas Thrift |
154 |
<< "<!--" << comment << "-->\n" |
179 |
Douglas Thrift |
144 |
<< "<page>\n" |
180 |
|
|
<< "\t<title>Sitemap</title>\n" |
181 |
|
|
<< "\t<section>\n" |
182 |
|
|
<< "\t\t<list>\n"; |
183 |
|
|
|
184 |
Douglas Thrift |
153 |
for (unsigned index(0); index < pages.size(); ++index) |
185 |
Douglas Thrift |
140 |
{ |
186 |
Douglas Thrift |
143 |
if (newPages.find(pages[index].getAddress()) != newPages.end()) |
187 |
|
|
{ |
188 |
|
|
newMap(pages[index].getChildren(), pages[index].getPath(), |
189 |
|
|
newPages.find(pages[index].getAddress())->second); |
190 |
|
|
} |
191 |
|
|
|
192 |
Douglas Thrift |
144 |
fout << pages[index](3) << '\n'; |
193 |
Douglas Thrift |
140 |
} |
194 |
Douglas Thrift |
144 |
|
195 |
|
|
fout << "\t\t</list>\n" |
196 |
|
|
<< "\t</section>\n" |
197 |
|
|
<< "</page>\n"; |
198 |
|
|
|
199 |
|
|
fout.close(); |
200 |
Douglas Thrift |
133 |
} |
201 |
Douglas Thrift |
143 |
|
202 |
|
|
void SiteMapper::newMap(vector<Page>& pages, const string& childOf, |
203 |
|
|
multimap<string, Page>& newPages) |
204 |
|
|
{ |
205 |
Douglas Thrift |
153 |
for (unsigned index(0); index < pages.size(); ++index) |
206 |
Douglas Thrift |
143 |
{ |
207 |
|
|
newMap(pages[index].getChildren(), pages[index].getPath(), newPages); |
208 |
|
|
} |
209 |
|
|
|
210 |
Douglas Thrift |
153 |
for (multimap<string, Page>::iterator itor(newPages.lower_bound(childOf)); |
211 |
Douglas Thrift |
143 |
itor != newPages.upper_bound(childOf); itor++) |
212 |
|
|
{ |
213 |
Douglas Thrift |
156 |
cout << "Added: " << itor->second.getUrl() << '\n'; |
214 |
|
|
|
215 |
Douglas Thrift |
143 |
pages.push_back(itor->second); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
newPages.erase(childOf); |
219 |
|
|
} |