8 from xml.dom.minidom import Document
11 import xml.etree.ElementTree as ElementTree
14 import cElementTree as ElementTree
17 import elementtree.ElementTree as ElementTree
19 import lxml.etree as ElementTree
24 from BeautifulSoup import BeautifulSoup
25 except ImportError, E:
28 feedName = "example-list.xml"
29 feedPath = "http://openlayers.org/dev/examples/"
31 def getListOfOnlineExamples(baseUrl):
33 useful if you want to get a list of examples a url. not used by default.
35 html = urllib2.urlopen(baseUrl)
36 soup = BeautifulSoup(html)
37 examples = soup.findAll('li')
38 examples = [example.find('a').get('href') for example in examples]
39 examples = [example for example in examples if example.endswith('.html')]
40 examples = [example for example in examples]
43 def getListOfExamples(relPath):
45 returns list of .html filenames within a given path - excludes example-list.html
47 examples = os.listdir(relPath)
48 examples = [example for example in examples if example.endswith('.html') and example != "example-list.html"]
52 def getExampleHtml(location):
54 returns html of a specific example that is available online or locally
57 if location.startswith('http'):
58 return urllib2.urlopen(location).read()
66 def extractById(soup, tagId, value=None):
68 returns full contents of a particular tag id
70 beautifulTag = soup.find(id=tagId)
72 if beautifulTag.contents:
73 value = str(beautifulTag.renderContents()).strip()
74 value = value.replace('\t','')
75 value = value.replace('\n','')
78 def getRelatedClasses(html):
80 parses the html, and returns a list of all OpenLayers Classes
81 used within (ie what parts of OL the javascript uses).
83 rawstr = r'''(?P<class>OpenLayers\..*?)\('''
84 return re.findall(rawstr, html)
86 def parseHtml(html,ids):
88 returns dictionary of items of interest
90 soup = BeautifulSoup(html)
93 d[tagId] = extractById(soup,tagId)
94 #classes should eventually be parsed from docs - not automatically created.
95 classes = getRelatedClasses(html)
96 d['classes'] = classes
100 h = os.popen("svn info %s --xml" % path)
101 tree = ElementTree.fromstring(h.read())
104 'url': tree.findtext('entry/url'),
105 'author': tree.findtext('entry/commit/author'),
106 'date': tree.findtext('entry/commit/date')
110 def createFeed(examples):
112 atomuri = "http://www.w3.org/2005/Atom"
113 feed = doc.createElementNS(atomuri, "feed")
114 feed.setAttribute("xmlns", atomuri)
115 title = doc.createElementNS(atomuri, "title")
116 title.appendChild(doc.createTextNode("OpenLayers Examples"))
117 feed.appendChild(title)
118 link = doc.createElementNS(atomuri, "link")
119 link.setAttribute("rel", "self")
120 link.setAttribute("href", feedPath + feedName)
122 modtime = time.strftime("%Y-%m-%dT%I:%M:%SZ", time.gmtime())
123 id = doc.createElementNS(atomuri, "id")
124 id.appendChild(doc.createTextNode("%s%s#%s" % (feedPath, feedName, modtime)))
127 updated = doc.createElementNS(atomuri, "updated")
128 updated.appendChild(doc.createTextNode(modtime))
129 feed.appendChild(updated)
131 examples.sort(key=lambda x:x["modified"])
132 for example in sorted(examples, key=lambda x:x["modified"], reverse=True):
133 entry = doc.createElementNS(atomuri, "entry")
135 title = doc.createElementNS(atomuri, "title")
136 title.appendChild(doc.createTextNode(example["title"] or example["example"]))
137 entry.appendChild(title)
139 link = doc.createElementNS(atomuri, "link")
140 link.setAttribute("href", "%s%s" % (feedPath, example["example"]))
141 entry.appendChild(link)
143 summary = doc.createElementNS(atomuri, "summary")
144 summary.appendChild(doc.createTextNode(example["shortdesc"] or example["example"]))
145 entry.appendChild(summary)
147 updated = doc.createElementNS(atomuri, "updated")
148 updated.appendChild(doc.createTextNode(example["modified"]))
149 entry.appendChild(updated)
151 author = doc.createElementNS(atomuri, "author")
152 name = doc.createElementNS(atomuri, "name")
153 name.appendChild(doc.createTextNode(example["author"]))
154 author.appendChild(name)
155 entry.appendChild(author)
157 id = doc.createElementNS(atomuri, "id")
158 id.appendChild(doc.createTextNode("%s%s#%s" % (feedPath, example["example"], example["modified"])))
159 entry.appendChild(id)
161 feed.appendChild(entry)
163 doc.appendChild(feed)
166 def wordIndex(examples):
168 Create an inverted index based on words in title and shortdesc. Keys are
169 lower cased words. Values are dictionaries with example index keys and
173 unword = re.compile("\\W+")
174 keys = ["shortdesc", "title"]
175 for i in range(len(examples)):
177 text = examples[i][key]
179 words = unword.split(text)
183 if index.has_key(word):
184 if index[word].has_key(i):
192 if __name__ == "__main__":
195 print "This script requires simplejson and BeautifulSoup. You don't have them. \n(%s)" % E
198 if len(sys.argv) > 1:
199 outFile = open(sys.argv[1],'w')
201 outFile = open('../examples/example-list.js','w')
203 examplesLocation = '../examples'
204 print 'Reading examples from %s and writing out to %s' % (examplesLocation, outFile.name)
207 docIds = ['title','shortdesc']
209 #comment out option to create docs from online resource
210 #examplesLocation = 'http://svn.openlayers.org/sandbox/docs/examples/'
211 #examples = getListOfOnlineExamples(examplesLocation)
213 examples = getListOfExamples(examplesLocation)
215 modtime = time.strftime("%Y-%m-%dT%I:%M:%SZ", time.gmtime())
217 for example in examples:
218 url = os.path.join(examplesLocation,example)
219 html = getExampleHtml(url)
220 tagvalues = parseHtml(html,docIds)
221 tagvalues['example'] = example
224 tagvalues["modified"] = d["date"] or modtime
225 tagvalues["author"] = d["author"] or "anonymous"
226 tagvalues['link'] = example
228 exampleList.append(tagvalues)
232 exampleList.sort(key=lambda x:x['example'].lower())
234 index = wordIndex(exampleList)
236 json = simplejson.dumps({"examples": exampleList, "index": index})
237 #give the json a global variable we can use in our js. This should be replaced or made optional.
238 json = 'var info=' + json
242 print "writing feed to ../examples/%s " % feedName
243 atom = open('../examples/%s' % feedName, 'w')
244 doc = createFeed(exampleList)
245 atom.write(doc.toxml())