Skip to content

Instantly share code, notes, and snippets.

@evandrix
Created July 21, 2011 13:17
Show Gist options
  • Save evandrix/1097155 to your computer and use it in GitHub Desktop.
Save evandrix/1097155 to your computer and use it in GitHub Desktop.
TracPlugin: "Hello World" (/helloworld-plugin) @ http://trac-hacks.org/wiki/EggCookingTutorialTrac0.11
# Helloworld module
from helloworld import *
=== Directory tree structure
+-helloworld-plugin/
|
+-helloworld/
| |
| +-__init__.py
| |
| +-templates/
| | |
| | +-helloworld.html
| |
| +-helloworld.py
| |
| +-htdocs/
| |
| +-css/
| | |
| | +-helloworld.css
| |
| +-images/
| |
| +-helloworld.png
|
+-setup.py
=== Deployment
1. Compile into an egg using `leewei@missquick:~/Downloads/helloworld-plugin$ python setup.py bdist_egg`
2. Move to suitable location: `leewei@missquick:~/Downloads/helloworld-plugin$ mv dist/*.egg ~/Downloads/my-project/plugins`
div.helloworld h1 {
color: red;
}
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="layout.html" />
<head>
<title>Helloworld</title>
</head>
<body>
<div id="ctxtnav" class="nav"></div>
<div id="content" class="helloworld">
<h1>Hello World!</h1>
<img src="${href.chrome('/hw/images/helloworld.png')}" />
</div>
</body>
</html>
# Helloworld plugin
import re
from genshi.builder import tag
from trac.core import *
from trac.web import IRequestHandler
from trac.web.chrome import INavigationContributor, ITemplateProvider, \
add_stylesheet
class HelloworldModule(Component):
implements(INavigationContributor, ITemplateProvider, IRequestHandler)
def process_request(self, req):
data = {}
add_stylesheet(req, 'hw/css/helloworld.css')
# This tuple is for Genshi (template_name, data, content_type)
# Without data the trac layout will not appear.
return 'helloworld.html', data, None
# INavigationContributor methods
def get_active_navigation_item(self, req):
return 'helloworld'
def get_navigation_items(self, req):
yield ('mainnav', 'helloworld',
tag.a('Hello World', href=req.href.helloworld()))
# IRequestHandler methods
def match_request(self, req):
return re.match(r'/helloworld(?:_trac)?(?:/.*)?$', req.path_info)
# ITemplateProvider methods
# Used to add the plugin's templates and htdocs
def get_templates_dirs(self):
from pkg_resources import resource_filename
return [resource_filename(__name__, 'templates')]
def get_htdocs_dirs(self):
"""Return a list of directories with static resources (such as style
sheets, images, etc.)
Each item in the list must be a `(prefix, abspath)` tuple. The
`prefix` part defines the path in the URL that requests to these
resources are prefixed with.
The `abspath` is the absolute path to the directory containing the
resources on the local file system.
"""
from pkg_resources import resource_filename
return [('hw', resource_filename(__name__, 'htdocs'))]
from setuptools import find_packages, setup
setup(
name='TracHelloWorld', # can by any name => used to create .egg
version='1.1',
packages=find_packages(exclude=['*.tests*']),
entry_points = """
[trac.plugins]
helloworld = helloworld
""",
package_data={'helloworld': ['templates/*.html',
'htdocs/css/*.css',
'htdocs/images/*']},
)
#from setuptools import setup
#PACKAGE = 'TracHelloWorld'
#VERSION = '0.1'
#setup(name=PACKAGE,
# version=VERSION,
# packages=['helloworld'],
# entry_points={'trac.plugins': '%s = helloworld' % PACKAGE},
# package_data={'helloworld': ['templates/*.html']}
#)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment