Code Monkey home page Code Monkey logo

anchorman's Introduction

Anchorman

.

Turn your text into hypertext. With anchorman you can markup anything, create anchors, links, annotate abbreviations or just highlight specific areas.

.

Example

>>> import anchorman
>>> text = "The quick brown fox jumps over the lazy dog."
>>> links = [{'fox': {'value': '/wiki/fox'}},
             {'dog': {'value': '/wiki/dog'}}]
>>> a = anchorman.add(text, links)
>>> print a
The quick brown <a href="/wiki/fox" class="anchorman">fox</a> jumps over
the lazy <a href="/wiki/dog" class="anchorman">dog</a>.

See more examples section at bottom.

.

Installation

Install anchorman via pip, master branch status: Built Status

pip install anchorman

or from source code

git clone https://github.com/tarnacious/anchorman.git && cd anchorman
python setup.py install

test it in a virtual environment

virtualenv .
./bin/pip install -r requirements.txt
./bin/py.test test

or locally

py.test --cov=anchorman --cov-report term-missing -v -s

.

More examples

Links with specific attributes Define general attribute value pairs on markup_format level and more specific attributes at the level of each link element.

>>> markup_format = {
        'tag': 'a',
        'value_key': 'href',
        'attributes': [
            ('style', 'color:blue;cursor:pointer;'),
            ('class', 'anchorman')
        ]
     }
>>> links = [{
        'red fox': {
            'value': '/redfox',
            'attributes': [
                ('class', 'animal'),
                ('style', 'font-size:23px;background:red'),
                ('title', 'Fix und Foxi')
            ]
        }
    }]
>>> a = anchorman.add(text, links, markup_format=markup_format)
The quick brown fox jumps over the lazy dog while the <a href="/redfox"
style="color:blue;cursor:pointer; font-size:23px;background:red"
class="anchorman animal" title="Fix und Foxi">red fox</a> sleeps.

.

Highlighting context Highlighting a term with pre- and postfix, e.g. variables in templates or low level tags.

>>> links = [{'fox': {}}]
>>> markup_format = {
        'highlighting': {
            'pre': '${{',
            'post': '}}'
        }
    }
>>> a = anchorman.add(text, links, markup_format=markup_format)
>>> print a
The quick brown ${{fox}} jumps over the lazy dog while the red
${{fox}} sleeps.

.

Parameters

markup_format: tag, value attribute or highlighting with pre- and postfix

replaces_per_item: default is replace all occurences, otherwise int

case-sensitive: default is True, set False to replace: Fox, fox and FOX

rm-identifier: create a specific identifier per set to delete them later

. Performance ===========

1000 items were processed with mean text len of 1800 characters. Same list of links was applied and around 11 times per text items augemented.

# without markup
min  0.00060 s
max  0.00891 s
mean 0.00152 s

# with basic markup
min  0.00061 s
max  0.00929 s
mean 0.00158 s

# highlighting
min  0.00057 s
max  0.00783 s
mean 0.00117 s

It is pretty fast already, but we need to check with larger link list and also some other cases > this was add links only, what about remove, apply other markup in between etc.

.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.