def create_link(self, text): components = text.split("|") ## mailto? if text.find("@") > 0: if len(components) == 1: return '<a href="mailto:%s" class="mail">%s</a>' % (text, text) elif len(components) == 2: return '<a href=mailto:"%s" class="mail">%s</a>' % (components[0], components[1]) else: return text # External link? if NewtonUtils.is_external_link(components[0]): if len(components) == 2: return '<a href="%s" class="urlextern" >%s</a>' % (components[0], self.strip_html(components[1])) elif len(components) == 1: return '<a href="%s">%s</a>' % (text, text) else: return text else: # local wiki link if len(components) == 2: return ' <a href="%s" class="%s">%s</a>' % ("data/" + components[0].strip(), self.page_exists_class(components[0]), self.strip_html(components[1])) elif len(components) == 1: if text.strip()[0] == '!': # it is an escaped CamelCase word. return '%s' % text.strip()[1:] # lose the ! and return it with no markup. return ' <a href="%s" class="%s">%s</a>' % ("data/" + text.strip(), self.page_exists_class(text.strip()), text.strip()) else: return text
def get_wiki_content(self, pagename): location = os.path.join(WIKI_DATA, pagename) data = self.parser.parse_wiki_text(NewtonUtils.get_file_contents(location), pagename) # print data[0] content = "%s%s%s" % (NewtonWikiText.WIKI_PAGE_HEADER, data[0], NewtonWikiText.WIKI_PAGE_FOOTER) content = content.replace("@PAGENAME@", pagename) content = content.replace("@MODDATE@", data[1]) content = content.replace("@PIXMAPS_DIR@", PIXMAPS_DIR) content = content.replace("@STYLESHEET@", os.path.join(WIKI_STYLES, self.config.get_string(KEY_STYLESHEET, "Blue") + ".css")) return (location, content)
def get_category_listing(self, catnode): list = [] catpath = NewtonUtils.path_from_nodename(catnode) self.get_category_subtree(catpath, list) levels = 0 content = '<h1>Category Contents</h1><div class="level1">\n<p>The following is a listing of all pages and categories within the category <code>%s</code>.' % catnode for item in list: relpath = item.replace(catpath, '') level = relpath.count('/') nodename = catnode + item.replace(catpath, '') if level == levels: if os.path.isdir(item): content += '<li class="category"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li>\n' % ('data/' + nodename.replace("/", '::'), os.path.basename(item)) else: content += '<li class="page"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li>\n' % ('data/' + nodename.replace("/", '::'), os.path.basename(item)) elif level > levels: if os.path.isdir(item): content += '\n<ul class="catlist">\n<li class="category"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li>\n' % ('data/' + nodename.replace('/', '::'), os.path.basename(item)) else: content += '\n<ul class="catlist">\n<li class="page"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li>\n' % ('data/' + nodename.replace('/', '::'), os.path.basename(item)) else: content += '\n</ul>\n' * (levels - level) if os.path.isdir(item): content += '<li class="category"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li>\n' % ('data/' + nodename.replace('/', '::'), os.path.basename(item)) else: content += '<li class="page"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li>\n' % ('data/' + nodename.replace('/', '::'), os.path.basename(item)) levels = level content += '\n</ul>\n' * (levels) content += '</div>' if not len(list): content += _('<div class="level2">\n<p>\n<strong>This category is empty.</strong>\n</p>\n</div>') content = "%s%s%s" % (NewtonWikiText.WIKI_PAGE_HEADER, content, NewtonWikiText.WIKI_PAGE_FOOTER) if catnode == '': content = content.replace("@PAGENAME@", 'WikiRoot') else: content = content.replace("@PAGENAME@", catnode) content = content.replace("@MODDATE@", 'N/A') content = content.replace("@PIXMAPS_DIR@", PIXMAPS_DIR) content = content.replace("@STYLESHEET@", os.path.join(WIKI_STYLES, self.config.get_string(KEY_STYLESHEET, 'Blue') + ".css")) return content
def get_search_results(self, pattern): results = [] for root, dirs, files in os.walk(WIKI_DATA): for file in files: path = os.path.join(root, file) contents = NewtonUtils.get_file_contents(path) if pattern.lower() in contents.lower(): page = path.replace(WIKI_DATA, '').replace('/', '::') results.append(page) if len(results): result_html = '<p><ol>\n' for result in results: result_html += '<div class="level2"><li class="level2"><span class="li"><a href="%s" class="wikilink1">%s</a></span></li></div>' % ("data/" + result, result) result_html += '</ol></p>' else: result_html = "<p>No results found.</p>" content = "%s%s%s" % (NewtonWikiText.WIKI_PAGE_HEADER, result_html, NewtonWikiText.WIKI_PAGE_FOOTER) content = content.replace("@PAGENAME@", 'Search Results: %s' % pattern) content = content.replace("@MODDATE@", 'N/A') content = content.replace("@PIXMAPS_DIR@", PIXMAPS_DIR) content = content.replace("@STYLESHEET@", os.path.join(WIKI_STYLES, self.config.get_string(KEY_STYLESHEET, 'Blue') + ".css")) return content
def get_raw_wiki_text(self, pagename): location = os.path.join(WIKI_DATA, pagename) raw_text = NewtonUtils.get_file_contents(location) return raw_text
def media_exists_class(self, media): if NewtonUtils.check_path_exists(media): return "wikilink3" else: return "wikilink2"
def page_exists_class(self, page): if NewtonUtils.check_path_exists(NewtonUtils.path_from_nodename(page)): return "wikilink1" else: return "wikilink2"
def __init__(self, config): self.config = config if not NewtonUtils.check_path_exists(WIKI_DIR): NewtonUtils.make_path(WIKI_DIR) if not NewtonUtils.check_path_exists(WIKI_DATA): NewtonUtils.make_path(WIKI_DATA) if not NewtonUtils.check_path_exists(WIKI_MEDIA): NewtonUtils.make_path(WIKI_MEDIA) if not NewtonUtils.check_path_exists(WIKI_STYLES): NewtonUtils.make_path(WIKI_STYLES) if not NewtonUtils.check_path_exists(WIKI_HOME): NewtonUtils.write_wiki_file(os.path.join(WIKI_DATA, 'NewtonHome'), NewtonWikiText.NEWTON_HOME_TEXT) # we have to overwrite the default styles and NewtonSyntax to allow extra features NewtonUtils.write_wiki_file(os.path.join(WIKI_STYLES, 'Human.css'), NewtonUtils.get_file_contents(os.path.join(DATA_DIR, 'Human.css')).replace("@PIXMAPS_DIR@", PIXMAPS_DIR)) NewtonUtils.write_wiki_file(os.path.join(WIKI_STYLES, 'Blue.css'), NewtonUtils.get_file_contents(os.path.join(DATA_DIR, 'Blue.css')).replace("@PIXMAPS_DIR@", PIXMAPS_DIR)) NewtonUtils.write_wiki_file(os.path.join(WIKI_DATA, 'NewtonSyntax'), NewtonWikiText.NEWTON_SYNTAX_TEXT) self.parser = TextParser(config) self.history = History()