Beispiel #1
0
    def transform(self, text, entry, *args):

        acros = self.acronyms
        if len(args) > 0:
            acros = dict(filter(lambda k: any(k[0] == v for v in args), iteritems(acros)))

        try:
            abbr = re.compile(r'\b(%s)\b' % '|'.join((pat.pattern for pat in acros)))
        except re.error as e:
            log.warn("acronyms: %s", e.args[0])

        def repl(match):

            abbr = match.group(0)
            desc = acros.get(abbr, None)

            if desc is None:
                for pat in acros:
                    if pat.match(abbr):
                        desc = acros.get(pat)
                        break
            return '<abbr title="%s">%s</abbr>' % (desc, abbr)

        try:
            return ''.join(Acrynomify(text, abbr, repl).result)
        except:
            log.exception('could not acronymize ' + entry.filename)
            return text
Beispiel #2
0
    def transform(self, text, entry, *args):

        acros = self.acronyms
        if len(args) > 0:
            acros = dict(
                filter(lambda k: any(k[0] == v for v in args),
                       iteritems(acros)))

        try:
            abbr = re.compile(r'\b(%s)\b' % '|'.join(
                (pat.pattern for pat in acros)))
        except re.error as e:
            log.warn("acronyms: %s", e.args[0])

        def repl(match):

            abbr = match.group(0)
            desc = acros.get(abbr, None)

            if desc is None:
                for pat in acros:
                    if pat.match(abbr):
                        desc = acros.get(pat)
                        break
            return '<abbr title="%s">%s</abbr>' % (desc, abbr)

        try:
            return ''.join(Acrynomify(text, abbr, repl).result)
        except HTMLParseError:
            log.exception('could not acronymize ' + entry.filename)
            return text
Beispiel #3
0
    def generate(item):

        entry = {}
        for k, v in iteritems({
                'title': 'title',
                'date': 'pubDate',
                'link': 'link',
                'content': 'description'
        }):
            try:
                entry[k] = item.find(v).text if k != 'content' \
                                             else unescape(item.find(v).text)
            except (AttributeError, TypeError):
                pass

        if any(
                filter(lambda k: k not in entry,
                       ['title', 'date', 'link', 'content'])):
            raise AcrylamidException('invalid RSS 2.0 feed: provide at least title, ' \
                                     + 'link, content and pubDate!')

        return {
            'title': entry['title'],
            'content': entry['content'],
            'date': parse_date_time(entry['date']),
            'link': entry['link'],
            'tags': [cat.text for cat in item.findall('category')]
        }
Beispiel #4
0
 def handle_data(self, data):
     if any(
             filter(lambda i: i in self.stack,
                    ['pre', 'code', 'math', 'script'])):
         pass
     else:
         data = self.abbr.sub(self.repl, data)
     self.result.append(data)
Beispiel #5
0
    def __getitem__(self, item):

        try:
            f = next(filter(lambda x: item in x.match, self))
        except StopIteration:
            raise ValueError('%s is not in list' % item)

        return f
Beispiel #6
0
    def handle_data(self, data):
        """Hyphenate words longer than 10 characters."""

        if any(filter(lambda i: i in self.stack, ['pre', 'code', 'math', 'script'])):
            pass
        else:
            split = [word for word in re.split(r"[.:,\s!?+=\(\)/-]+", data)
                     if len(word) > self.length]
            for word in split:
                hyphenated = '&shy;'.join(self.hyphenate(word))
                data = data.replace(word, hyphenated)

        self.result.append(data)
Beispiel #7
0
def find(fname, directory):
    """Find `fname` in `directory`, if not found try the parent folder until
    we find `fname` (as full path) or raise an :class:`IOError`."""

    directory = directory.rstrip('/')

    while directory:
        try:
            return os.path.join(directory, next(filter(
                lambda p: p == fname, os.listdir(directory))))
        except (OSError, StopIteration):
            directory = directory.rsplit('/', 1)[0]
    else:
        raise IOError
Beispiel #8
0
    def handle_data(self, data):
        """Hyphenate words longer than 10 characters."""

        if any(
                filter(lambda i: i in self.stack,
                       ['pre', 'code', 'math', 'script'])):
            pass
        else:
            split = [
                word for word in re.split(r"[.:,\s!?+=\(\)/-]+", data)
                if len(word) > self.length
            ]
            for word in split:
                hyphenated = '&shy;'.join(self.hyphenate(word))
                data = data.replace(word, hyphenated)

        self.result.append(data)
Beispiel #9
0
    def generate(item):

        entry = {}
        for k, v in iteritems({'title': 'title', 'date': 'pubDate',
                               'link': 'link', 'content': 'description'}):
            try:
                entry[k] = item.find(v).text if k != 'content' \
                                             else unescape(item.find(v).text)
            except (AttributeError, TypeError):
                pass

        if any(filter(lambda k: k not in entry, ['title', 'date', 'link', 'content'])):
            raise AcrylamidException('invalid RSS 2.0 feed: provide at least title, ' \
                                     + 'link, content and pubDate!')

        return {'title': entry['title'],
               'content': entry['content'],
               'date': parse_date_time(entry['date']),
               'link': entry['link'],
               'tags': [cat.text for cat in item.findall('category')]}
Beispiel #10
0
 def handle_data(self, data):
     if any(filter(lambda i: i in self.stack, ['pre', 'code', 'math', 'script'])):
         pass
     else:
         data = self.abbr.sub(self.repl, data)
     self.result.append(data)