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