8 from lxml.builder import ElementMaker
10 from treecutter import constants as const
11 from treecutter.trie import Trie
12 from treecutter.link import Link
13 from treecutter.tools import ssh_cmd, publish, mkdir_p
16 """Class keeping the internal site structure"""
17 def __init__(self,args):
18 self._output = args.output
19 self._style = args.style
20 self._subdir = args.subdir
21 self._file = 'sitemap.txt'
23 self._sitelang = set()
24 self._isocode = etree.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
26 self._tmptarget = tempfile.mkdtemp()+'/'
28 # The sitemap uses a trie structure to keep track of links
29 # A link represents the path to the document and the link
30 # representing the text on the site.
31 # A link can have several pages in different languages.
32 def add_link(self, link):
33 tokens = filter(None,re.split(r'(^/[\w-]*/|[\w-]*/)',link))
34 self._tree.add(tokens,Link(link))
37 f = open(self._file,'w')
38 f.write('\n'.join(link.link() for link in self._tree))
44 sml = f.read().split()
48 except IOError, what_error:
49 print 'INFO: Could not read sitemap.txt - one will be created'
51 # Create a set of the current tree for comparison with the
54 return set(link.link() for link in self._tree)
56 # Main driver in the application processing the documents
57 # in the collected sitemap
60 print "Prepareing the input"
61 for link in self._tree:
64 print "Prepare [%5.2f s]" % (round(t2-t1,2))
65 for link in self._tree:
66 self._sitelang = self._sitelang.union(set(link.languages()))
67 for tran in self._sitelang:
69 self._tranlang[tran] = gettext.translation('iso_639_3',
72 print "Language [%5.2f s]" % (round(t3-t2,2))
73 for link in self._tree:
74 link.render(self._style)
76 print "Render [%5.2f s]" % (round(t4-t3,2))
77 for link in self._tree:
78 link.template(self, self._style, self._tmptarget)
80 print "Template [%5.2f s]" % (round(t5-t4,2))
83 # Collect all files used by the documents
84 for link in self._tree:
85 res = res.union(link.resources())
87 outfile = self._tmptarget+f
88 mkdir_p(os.path.dirname(outfile))
89 shutil.copyfile(f,outfile)
90 print "Resources[%5.2f s]" % (round(t6-t5,2))
91 # TODO: Improve the sitemap, it is a page that is generated from
92 # the ground up and added a bit adhoc.
93 sitmaplink = Link('/sitemap')
94 for l in self._sitelang:
95 sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
96 for l in self._sitelang:
97 sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
98 sitmaplink.page(l).template(self,self._style,self._tmptarget)
100 print "Sitemap [%5.2f s]" % (round(t7-t6,2))
105 def gen_menu(self,lang,page,cssclass):
106 return self._tree.menu(lang,page,cssclass,self._subdir)
108 def lang_menu(self,lang,link):
109 html = ElementMaker()
111 for l in link.languages():
112 isoxml = u"//iso_639_3_entry[@*='"+l+"']"
113 ln = self._isocode.xpath(isoxml)[0].get('name')
115 ln = self._tranlang[lang].gettext(ln)
120 li = html.li(html.a(ln.decode('utf-8'),
121 href=self._subdir+p,hreflang=l))
123 return etree.tostring(menu,encoding='UTF-8',pretty_print=False)
126 ssh_cmd(self._output,"mkdir -p")
127 publish(self._tmptarget, self._output)
128 for res in ["css","images","js","fonts","favicon.ico"]:
129 if (os.path.exists(self._style+res)):
130 publish(self._style+res, self._output)
131 ssh_cmd(self._output,"chmod a+rx")