def add_user_style(self): # tested '''adds user style meant to replace Crunchy's default if so desired by the user''' if 'my_style' not in config: # should normally be found return if not config['my_style']: return if 'styles' not in config: # should normally be found return else: styles = config['styles'] if styles == {}: return style = et.Element("style", type="text/css") style.text = '' for key in styles: if key != 'name': style.text += key + "{" + styles[key] + "}\n" try: self.head.append( style) # has to appear last to override all others. except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.append(style) return
def insert_css_file(self, path): '''inserts a link to the standard Crunchy style file''' css = et.Element("link", type="text/css", rel="stylesheet", href=path) try: self.head.append(css) except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.append(css) return
def add_charset(self): # tested '''adds utf-8 charset information on a page''' meta = et.Element("meta", content="text/html; charset=UTF-8") meta.set("http-equiv", "Content-Type") try: self.head.append(meta) except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.append(meta) return
def add_css_code(self, code): # tested '''inserts styling code in <head>''' css = et.Element("style", type="text/css") css.text = code try: self.head.insert(0, css) except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.insert(0, css) return
def add_js_code(self, code): # tested ''' includes some javascript code in the <head>. This is the preferred method.''' js = et.Element("script", type="text/javascript") js.text = code try: self.head.append(js) except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.append(js) return
def insert_js_file(self, filename): # tested '''Inserts a javascript file link in the <head>. This should only be used for really big scripts (like editarea); the preferred method is to add the javascript code directly''' js = et.Element("script", src=filename, type="text/javascript") js.text = " " # prevents premature closing of <script> tag, misinterpreted by Firefox try: self.head.insert(0, js) except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.insert(0, js) return
def add_crunchy_style(self): # tested '''inserts a link to the standard Crunchy style file''' css = et.Element("link", type="text/css", rel="stylesheet", href="/css/crunchy.css") try: self.head.insert(0, css) except: # should never be needed in normal call from CrunchyPage self.find_head() self.head.insert(0, css) # we inserted first so that it can be overriden by tutorial writer's # style and by user's preferences. return
def find_head(self): # tested '''finds the head in an html tree; adds one in if none is found. ''' self.head = self.tree.find("head") if self.head is None: self.head = et.Element("head") self.head.text = " " html = self.tree.find("html") try: html.insert(0, self.head) except AttributeError: html = self.tree.getroot() assert html.tag == 'html' html.insert(0, self.head) return
def __init__(self, filehandle, url, username=None, remote=False, local=False): """ read a page, processes it and outputs a completely transformed one, ready for display in browser. url should be just a path if crunchy accesses the page locally, or the full URL if it is remote. """ if username is None: username = interface.unknown_user BasePage.__init__(self, username=username) self.url = url # Assign tutorial type self.is_remote = remote # True if remote tutorial, on the web self.is_local = local # True if local tutorial, not from the server root if not (remote or local): self.is_from_root = True # if local tutorial from server root else: self.is_from_root = False # Create the proper tree structure from the html file try: self.create_tree(filehandle) # assigns self.tree except: # reports formatted traceback in browser window text = changeHTMLspecialCharacters(handle_exception(False)) self.tree = et.ElementTree( et.fromstring( "<html><head/><body><pre>%s</pre></body></html>" % text)) return # Removing pre-existing javascript, unwanted objects and # all kinds of other potential security holes remove_unwanted(self.tree, self) # from the security module self.find_head() # assigns self.head self.find_body() # assigns self.body # Crunchy's main work: processing vlam instructions, etc. try: self.process_tags() except Exception: self.body.clear() self.body.tag = "body" pre = et.Element('pre') pre.text = handle_exception(False) self.body.append(pre) return # adding the javascript for communication between the browser and the server self.insert_js_file("/javascript/jquery.js") self.add_js_code(comet_js % self.pageid) # Extra styling self.add_crunchy_style() # first Crunchy's style self.add_user_style() # user's preferences can override Crunchy's return