December 18, 2008
filed in the early evening by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 1:24 minutes

while setting up a company internal trac wiki for a small project i wanted to include a link to a relevant youtube video and started looking for a YouTube trac macro — without finding one really.1 in typical top gear fashion i asked myself “how hard can it be” to write my own trac macro. turns out: not very.

so, here it is:

from datetime import datetime
# Note: since Trac 0.11, datetime objects are used internally

from genshi.builder import tag

from trac.util.datefmt import format_datetime, utc
from trac.wiki.macros import WikiMacroBase
from trac.util.html import Markup

import re

class YouTubeMacro(WikiMacroBase):
    '''Simple YouTube macro.

    Use as follows:

        [[YouTube(YOUTUBE-URL)]]
    '''

    revision = '0.2'
    url = 'http://xyzzyxyzzy.net'

    reYouTube = re.compile(r'http://.+/watch\?v=(?P<id>.+)$')

    def expand_macro(self, formatter, name, args):
        # parse: http://uk.youtube.com/watch?v=kJNDcurLP1w
        id = args
        match = YouTubeMacro.reYouTube.match(id)
        if match:
            id = match.group('id')

        return Markup('''<div class="tracyoutube">
   <object width="425" height="344">
       <param name="movie" value="http://www.youtube.com/v/%(id)s&hl=en&fs=1"></param>
       <param name="allowFullScreen" value="true"></param>
       <param name="allowscriptaccess" value="always"></param>
       <embed src="http://www.youtube.com/v/%(id)s&hl=en&fs=1" type="application/x-shockwave-flash"
              allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
   </object>
</div>''' % dict(id = id))

cook for 5min, stir constantly, download to your trac’s plugin directory as YouTube.py and enjoy.


  1. the MovieMacro plugin looked like a good way of doing it, but apparently you need to install a special, embedded, flash player which i had no success with… 

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.

1 comment »

  1. Hello I found very cool your plugin but for me doesn’t really work, so I mean return like text, not really embedded video

    For Example [[YouTube(http://www.youtube.com/watch?v=DkK95byEhyE)]]

    Return:

    I tried too with {{{ #!html, but no chance,

    Any help,

    Thanks a lot,

    Guillermo

    comment by Guillermo Pared — April 23, 2010 @ 09:22

RSS feed for comments on this post. TrackBack URI

Leave a comment