Esempio n. 1
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        if (self.mtime < s.mtime(self.wiki_map)):
            self.load()

        try:
            uri = tag['href']
        except KeyError:
            return True
        try:      
            (schema, link) = uri.split(':',1)
        except ValueError:
            return False

        schema = schema.lower()
        tag['rel'] = uri

        if schema in self.schemas.keys():
            if '%s' in self.schemas[schema]:
                try:
                    uri = self.schemas[schema] % link
                except:
                    print "Error in processing Interwiki link (%s,%s,%s)" % (schema, link, self.schemas[schema])
                    uri = self.schemas[schema] + link
            else:
                uri = self.schemas[schema] + link
            tag['href'] = uri
            (schema,netloc,path,parameters,query,fragment) = urlparse.urlparse(uri)
            tag['title'] = "link to %s on %s" % (link, netloc)
            tag['class'] = "interwiki"
            # this tag does not need to be re-processed
            return False
Esempio n. 2
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        if (self.mtime < s.mtime(self.meta_page)):
            self.load()

        try:
            uri = tag['href']
        except KeyError:
            return True
        try:      
            (schema, link) = uri.split(':',1)
        except ValueError:
            return False

        schema = schema.lower()
        tag['rel'] = uri

        if schema in self.schemas.keys():
            if '%s' in self.schemas[schema]:
                try:
                    uri = self.schemas[schema] % link
                except:
                    log.error("Error in processing Interwiki link (%s,%s,%s)" % (schema, link, self.schemas[schema]))
                    uri = self.schemas[schema] + link
            else:
                uri = self.schemas[schema] + link
            tag['href'] = uri
            (schema,netloc,path,parameters,query,fragment) = urlparse.urlparse(uri)
            tag['title'] = "link to %s on %s" % (link, netloc)
            tag['class'] = "interwiki"
            # this tag does not need to be re-processed
            return False
Esempio n. 3
0
    def load(self):
        # load Aliases
        s = Store()
        try:
            page = s.get_page(self.meta_page)
        except:
            log.warn("Aliases: no %s definitions" % self.meta_page)
            return

        # prepare to parse only <pre> tags (so that we can have multiple maps organized by sections)
        soup = BeautifulSoup(render_markup(page['data'], page['content-type']))
        h = HTMLParser.HTMLParser()

        all_sections = u''.join(
            map(lambda t: str(t.string), soup.find_all('pre'))).strip()
        # now that we have the full map, let's build the schema hash
        for line in all_sections.split('\n'):
            try:
                (link, replacement) = line.strip().split(' ', 1)
                self.aliases[link] = replacement
                self.aliases[link.replace('_', ' ')] = replacement
            except ValueError:
                log.warn("skipping line '%s'" % line)
                pass
        self.mtime = time.time()
Esempio n. 4
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        try:
            source = tag['src']
            (schema,host,path,parameters,query,fragment) = urlparse.urlparse(source)
            if schema == 'cid' or s.is_attachment(pagename,path):
                filename = s.get_attachment_filename(pagename,path)
                if os.path.exists(filename):
                    buffer = codecs.open(filename,'r','utf-8').read().strip()
                else:
                    tag.replaceWith(_('error_include_file'))
                    return False
            else:
                tag.replaceWith(_('error_reference_format'))
                return False
        except KeyError:
            buffer = u''.join(tag.find_all(text=re.compile('.+'))).strip()
        try:
            lexer = tag['syntax'].lower()
        except KeyError:
            lexer = 'text'

        if request is False: # we're formatting for RSS
            lexer = 'text'

        lexer = get_lexer_by_name(lexer)
        formatter = HtmlFormatter(linenos=False, cssclass='syntax')
        result = highlight(buffer, lexer, formatter)
        tag.replace_with(BeautifulSoup(result.strip()))

        return False # no other plugin should process this tag
Esempio n. 5
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        try:
            source = tag['src']
            (schema, host, path, parameters, query,
             fragment) = urlparse.urlparse(source)
            if schema == 'cid' or s.is_attachment(pagename, path):
                filename = s.get_attachment_filename(pagename, path)
                if os.path.exists(filename):
                    buffer = codecs.open(filename, 'r', 'utf-8').read().strip()
                else:
                    tag.replaceWith(_('error_include_file'))
                    return False
            else:
                tag.replaceWith(_('error_reference_format'))
                return False
        except KeyError:
            buffer = u''.join(tag.find_all(text=re.compile('.+'))).strip()
        try:
            lexer = tag['syntax'].lower()
        except KeyError:
            lexer = 'text'

        if request is False:  # we're formatting for RSS
            lexer = 'text'

        lexer = get_lexer_by_name(lexer)
        formatter = HtmlFormatter(linenos=False, cssclass='syntax')
        result = highlight(buffer, lexer, formatter)
        tag.replace_with(BeautifulSoup(result.strip()))

        return False  # no other plugin should process this tag
Esempio n. 6
0
    def __init__(self, settings):
        """Initialize the controler and preload basic metadata"""

    	self.redis = Redis(host=settings.redis.bind_address, port=settings.redis.port)
    	self.store = Store(settings.content.path)
        self.get_all_pages()   # page modification times
        self.get_all_aliases() # page aliases
Esempio n. 7
0
def wiki(page):
    s = Store(path_for(settings.content.path))
    try:
        result = s.get_page(page.lower())
    except:
        result = s.get_page('meta/EmptyPage')

    return result
Esempio n. 8
0
def wiki(page):
    """Render a wiki page"""

    s = Store(path_for(settings.content.path))
    try:
        result = s.get_page(page.lower())
    except Exception, e:
        log.warn("%s rendering page %s" % (e, page))
        result = s.get_page('meta/EmptyPage')
Esempio n. 9
0
def wiki(page):
    """Render a wiki page"""

    s = Store(path_for(settings.content.path))
    try:
        result = s.get_page(page.lower())
    except Exception, e:
        log.warn("%s rendering page %s" % (e, page))
        result = s.get_page('meta/EmptyPage')
Esempio n. 10
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        if (self.mtime < s.mtime(self.meta_page)):
            self.load()
        try:
            acronym = ''.join(tag.find_all(text=re.compile('.+'))).strip().lower()
        except:
            return True

        if acronym in self.acronyms.keys():
            meaning = self.acronyms[acronym]
            tag['title'] = meaning
            # this tag does not need to be re-processed
            return False
        return True
Esempio n. 11
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        if (self.mtime < s.mtime(self.meta_page)):
            self.load()
        try:
            acronym = ''.join(
                tag.find_all(text=re.compile('.+'))).strip().lower()
        except:
            return True

        if acronym in self.acronyms.keys():
            meaning = self.acronyms[acronym]
            tag['title'] = meaning
            # this tag does not need to be re-processed
            return False
        return True
Esempio n. 12
0
class ImageWikiPlugin:
    __metaclass__ = Singleton

    category = 'markup'
    tags = ['img']

    def __init__(self):
        log.debug(self)

    def run(self, serial, tag, tagname, pagename, soup, request, response):
        try:
            uri = tag['src']
            schema, _, path, _, _, _ = urlparse.urlparse(uri)
        except Exception, e:
            log.debug(e)
            return True

        if schema:  # TODO: deal with cid: schema
            return True

        s = Store()
        if s.is_attachment(pagename, path):
            tag['src'] = unicode(
                cgi.escape(os.path.join(settings.wiki.media, pagename, path)))
            return False  # no further processing required
        return True
Esempio n. 13
0
    def load(self):
        # Load acronym map
        s = Store()
        try:
            page = s.get_page(self.meta_page)
        except:
            log.warn("no %s definitions" % meta_page)
            return

        # prepare to parse only <pre> tags (so that we can have multiple maps organized by sections)
        soup = BeautifulSoup(render_markup(page['data'],page['content-type']))

        all_sections = u''.join(map(lambda t: str(t.string), soup.find_all('pre'))).strip()
        # now that we have the full map, let's build the schema hash

        for line in all_sections.split('\n'):
            try:
                (acronym, expansion) = line.split(' ',1)
                self.acronyms[acronym.lower()] = expansion
            except ValueError: # skip blank lines or with more than two fields
                log.debug("skipping line '%s'" % line)
                pass
Esempio n. 14
0
    def load(self):
        # Load acronym map
        s = Store()
        try:
            page = s.get_page(self.meta_page)
        except:
            log.warn("no %s definitions" % meta_page)
            return

        # prepare to parse only <pre> tags (so that we can have multiple maps organized by sections)
        soup = BeautifulSoup(render_markup(page['data'], page['content-type']))

        all_sections = u''.join(
            map(lambda t: str(t.string), soup.find_all('pre'))).strip()
        # now that we have the full map, let's build the schema hash

        for line in all_sections.split('\n'):
            try:
                (acronym, expansion) = line.split(' ', 1)
                self.acronyms[acronym.lower()] = expansion
            except ValueError:  # skip blank lines or with more than two fields
                log.debug("skipping line '%s'" % line)
                pass
Esempio n. 15
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        if (self.mtime < s.mtime(self.meta_page)):
            self.load()

        try:
            link = tag['href']
        except KeyError:
            return True

        while True: # expand multiple aliases if required
            stack = [] # avoid loops
            try:
                alias = self.aliases[tag['href']]
                if alias not in stack:
                    stack.append(alias)
                    tag['href'] = alias
                else: # avoid loops
                    break
            except:
                break
        # this tag may need to be re-processed by another plugin
        return True
Esempio n. 16
0
    def load(self):
        # load InterWikiMap
        s = Store()
        try:
            page = s.get_page(self.meta_page)
        except:
            log.warn("InterWikiMap: no %s definitions" % self.meta_page)
            return

        # prepare to parse only <pre> tags (so that we can have multiple maps organized by sections)
        soup = BeautifulSoup(render_markup(page['data'],page['content-type']))
        h = HTMLParser.HTMLParser()

        all_sections = u''.join(map(lambda t: str(t.string), soup.find_all('pre'))).strip()
        # now that we have the full map, let's build the schema hash
        for line in all_sections.split('\n'):
            try:
                (schema, url) = line.strip().split(' ',1)
                self.schemas[schema.lower()] = h.unescape(url) #url.replace("&amp;","&")
            except ValueError:
                log.debug("skipping line '%s'" % line)
                pass
        self.mtime = time.time()
Esempio n. 17
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        s = Store()
        if (self.mtime < s.mtime(self.meta_page)):
            self.load()

        try:
            link = tag['href']
        except KeyError:
            return True

        while True:  # expand multiple aliases if required
            stack = []  # avoid loops
            try:
                alias = self.aliases[tag['href']]
                if alias not in stack:
                    stack.append(alias)
                    tag['href'] = alias
                else:  # avoid loops
                    break
            except:
                break
        # this tag may need to be re-processed by another plugin
        return True
Esempio n. 18
0
    def load(self):
        # load InterWikiMap
        s = Store()
        try:
            page = s.get_page(self.wiki_map)
        except:
            log.warn("InterWikiMap: no %s definitions" % self.wiki_map)
            return

        # prepare to parse only <pre> tags (so that we can have multiple maps organized by sections)
        soup = BeautifulSoup(render_markup(page['data'],page['content-type']))
        h = HTMLParser.HTMLParser()

        all_sections = u''.join(map(lambda t: str(t.string), soup.find_all('pre'))).strip()
        # now that we have the full map, let's build the schema hash
        for line in all_sections.split('\n'):
            try:
                (schema, url) = line.strip().split(' ',1)
                self.schemas[schema.lower()] = h.unescape(url) #url.replace("&amp;","&")
            except ValueError:
                log.warn("skipping line '%s'" % line)
                pass
        self.mtime = time.time()
Esempio n. 19
0
    def load(self):
        # load Aliases
        s = Store()
        try:
            page = s.get_page(self.meta_page)
        except:
            log.warn("Aliases: no %s definitions" % self.meta_page)
            return

        # prepare to parse only <pre> tags (so that we can have multiple maps organized by sections)
        soup = BeautifulSoup(render_markup(page['data'],page['content-type']))
        h = HTMLParser.HTMLParser()

        all_sections = u''.join(map(lambda t: str(t.string), soup.find_all('pre'))).strip()
        # now that we have the full map, let's build the schema hash
        for line in all_sections.split('\n'):
            try:
                (link, replacement) = line.strip().split(' ',1)
                self.aliases[link] = replacement
                self.aliases[link.replace('_',' ')] = replacement
            except ValueError:
                log.warn("skipping line '%s'" % line)
                pass
        self.mtime = time.time()
Esempio n. 20
0
class WikiController(object):

    def __init__(self, settings):
        """Initialize the controler and preload basic metadata"""

    	self.redis = Redis(host=settings.redis.bind_address, port=settings.redis.port)
    	self.store = Store(settings.content.path)
        self.get_all_pages()   # page modification times
        self.get_all_aliases() # page aliases


    def get_page(self, path):
        """Returns a single page"""

        if path in self.store.pages:
            return self.store.get_page(path)
        raise KeyError


    def resolve_alias(self, path):
        """Attempts to resolve an alias to a page"""

        # Check locally first, to save overhead
        if path in self.store.aliases:
            return self.store.aliases[path]

        # Check if there's been an update in Redis
        alias = self.redis.hget(META_ALIASES, path)
        if alias:
            self.store.aliases[path] = alias
            return alias
        
        return None


    def get_all_pages(self):
        """Returns a hash of all known pages and mtimes"""

        if not len(self.store.pages):
            if self.redis.exists(META_PAGES):
                self.store.pages = self.redis.hgetall(META_PAGES)
            else:
                # force filesystem scan and alias generation
                pages = self.store.get_all_pages()
                log.debug(pages)
                self.redis.hmset(META_PAGES,self.store.get_all_pages())
        return self.store.pages


    def get_all_aliases(self):
        """Returns a hash of all known page aliases"""

        if not len(self.store.aliases):
            if self.redis.exists(META_ALIASES):
                self.store.aliases = self.redis.hgetall(META_ALIASES)
            else:
                # force filesystem scan and alias generation
                self.store.get_all_pages()
                self.redis.hmset(META_ALIASES, self.store.aliases)
        return self.store.aliases


    def get_close_matches_for_page(self, path):
        """Get a list of close matches for a given page/path"""

        pages = self.get_all_pages()
        return get_close_matches(path, pages.keys())
Esempio n. 21
0
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        i, s = Index(), Store()

        try:
            uri = tag['href']
        except KeyError:
            return True

        # Try to handle relative URIs
        if uri[0] == '.':
            uri = posixpath.normpath(os.path.join(pagename, uri))

        # Try to handle the uri as a schema/path pair
        schema = ''
        path = uri
        try:
            schema, path = uri.split(':', 1)
        except:
            pass
        known = False

        if schema == '':
            uri = i.resolve_alias(path)

            if uri != path:
                path = tag['href'] = uri

            if s.exists(uri) or uri in i.all_pages:
                known = True

        if (schema == ''):
            if s.is_attachment(pagename, path):
                tag['href'] = unicode(self.media + pagename + "/" + path)
                tag['title'] = self.schemas['attach']['title'] % {
                    'uri': os.path.basename(path)
                }
                tag['class'] = self.schemas['attach']['class']
                return False

            if (
                    known
            ):  # this is a known Wiki link, so there is no need to run it through more plugins
                if request is False:
                    # check for a direct outbound link
                    if path in i.default_links:
                        uri = i.default_links[path]
                        (schema, netloc, path, parameters, query,
                         fragment) = urlparse.urlparse(uri)
                        tag['href'] = uri
                        tag['title'] = self.schemas[schema]['title'] % {
                            'uri': uri
                        }
                        tag['class'] = self.schemas[schema]['class']
                        return False
                tag['href'] = self.base + uri
                tag['class'] = "wiki"
                try:  # to use indexed metadata to annotate links
                    last = i.page_info[path]['last-modified']
                    tag['title'] = _('link_update_format') % (path,
                                                              time_since(last))
                except:
                    tag['title'] = _('link_defined_notindexed_format') % path
            elif ('#' in uri):
                # this is an anchor, leave it alone
                tag['href'] = self.base + uri
                tag['class'] = "anchor"
                try:
                    exists = tag['title']
                except:
                    tag['title'] = _('link_anchor_format') % fragment
            else:
                if request is False:
                    # remove unknown wiki links for RSS feeds
                    tag.replace_with(tag.contents[0])
                    # format for online viewing
                try:
                    exists = tag['class']
                    return True  #we're done here, but this tag may need handling elsewhere
                except:
                    tag['href'] = self.base + uri
                    tag['class'] = "wikiunknown"
                    tag['title'] = _('link_undefined_format') % path

        elif (schema in self.schemas.keys()
              ):  # this is an external link, so reformat it
            tag['title'] = self.schemas[schema]['title'] % {'uri': uri}
            tag['class'] = self.schemas[schema]['class']
            #tag['target'] = '_blank'
        else:  # assume this is an interwiki link (i.e., it seems to have a custom schema)
            tag['title'] = _('link_interwiki_format') % uri
            tag['class'] = "interwiki"
            tag['target'] = '_blank'
            # Signal that this tag needs further processing
            return True
        # We're done
        return False