ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/SiteMapper/SiteMapper.cpp
(Generate patch)

Comparing SiteMapper/SiteMapper.cpp (file contents):
Revision 249 by Douglas Thrift, 2004-09-11T23:21:11-07:00 vs.
Revision 637 by douglas, 2005-12-31T21:32:21-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines