[PYTHON] Easier div tags on the Trac wiki

Use of div tag

When writing a Wiki in Trac, for example, you may want to add a note like this. div-msg

The way to write this kind of thing is to prepare a style sheet in advance and

style.css


div[class|="msg"] {
    background-repeat: no-repeat;
    background-position: 5px 10px;
    border-top: solid 1px; 
    border-left: solid 10px; 
    padding: 3px 45px;
}
div.msg-devil {
    background-image: url("face-devilish.png ");
    border-color: red;
}

It uses a div processor.

{{{
#!div class="msg-devil"
'''I'm going to take the exam! !! !!'''
}}}

···Troublesome

It's not that bulky, but I want to write it in one line with something like this [[div (devil,'''I'm going to take the exam !!!''')]. So I made a simple plugin.

DivHelperPlugin

web_ui.py


# -*- coding:utf8 -*-
from trac.core import *
from trac.config import ConfigSection
from trac.wiki import IWikiMacroProvider
from trac.wiki.formatter import system_message
from trac.wiki.formatter import WikiProcessor
from trac.wiki.parser import parse_processor_args

class DivHelperPlugin(Component):
    "undocumented"
    implements (IWikiMacroProvider)

    _section = ConfigSection('divhelper', 'undocumented')
    _prefixes = {"div": "div-alias.", "span": "span-alias."}
    
    def get_macros(self):
        yield "cdiv"
        yield "cspan"
        for name, value in self._section.options():
            for pname, prefix in self._prefixes.items():
                if name.startswith(prefix):
                    yield pname + '_' + name[len(prefix):]

    def get_macro_description(self, name):
        return 'undocumented : %s' % name

    def expand_macro(self, formatter, name, content, args = None):
        if name in ("cdiv", "cspan"):
            try:
                clsname, content = content.split(",", 1)
                pname = name[1:]
                pargs = {"class": clsname}
                content = content.strip()
            except:
                return system_message("%s: invalid args (%s)" % (name, content))
        else:
            pname, alias = name.split("_", 1)
            key = self._prefixes[pname] + alias
            pargs = parse_processor_args(self._section.get(key))

        p = WikiProcessor(formatter, pname, pargs)
        return p.processor(content)

I'm afraid that there are a lot of omissions around get_macro_description ...

Installation

The source is here, so you can either hg clone and python setup.py bdist_egg or python setup.py install. https://bitbucket.org/iwata0303/tracdivhelper

Then follow the general Trac plugin installation instructions.

It only works with Trac 1.0. I think it will work if it is 0.12 series, but how about it?

How to use

Once installed, you will be able to use the macro cdev. With this, you can write the above.

[[cdev(msg-devil, '''I'm going to take the exam! !! !!''')]]

Also, if you write such settings in trac.ini in advance,

trac.ini


[divhelper]
div-alias.devil = class="msg-devil"

You will be able to write shorter.

[[div_devil('''I'm going to take the exam! !! !!''')]]

application

For example, if you want to make the text color red, you can usually install ColorMacro or use the span macro.

[[Color(none, red,It's a red letter)]]
[[span(It's a red letter, style=color:red)]]

I do something like this, but this is also this plug-in,

trac.ini


[divhelper]
span-alias.red = style="color:red"

You will be able to write more simply.

[[span_red(It's a red letter)]]

It's a plug-in with about 30 lines, and it doesn't take much time, but it's quite convenient :) (Self-praise)

Recommended Posts

Easier div tags on the Trac wiki
The problem becomes easier to solve depending on the formulation method