wiki:WikiMacros
close Warning: Error with navigation contributor "LoginModule"

Version 1 (modified by trac, 14 years ago) (diff)

--

Trac Macros

Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. A macro inserts dynamic HTML data in any context supporting WikiFormatting.

Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting).

Using Macros

Macro calls are enclosed in two square brackets. Like Python functions, macros can also have arguments, a comma separated list within parentheses.

Trac macros can also be written as TracPlugins. This gives them some capabilities that macros do not have, such as being able to directly access the HTTP request.

Example

A list of 3 most recently changed wiki pages starting with 'Trac':

 [[RecentChanges(Trac,3)]]

Display:

Available Macros

Note that the following list will only contain the macro documentation if you've not enabled -OO optimizations, or not set the PythonOptimize option for mod_python.

[[CommitTicketReference]]

I n s e r t a c h a n g e s e t m e s s a g e i n t o t h e o u t p u t . T h i s m a c r o m u s t b e c a l l e d u s i n g w i k i p r o c e s s o r s y n t a x a s f o l l o w s : { { { { { { # ! C o m m i t T i c k e t R e f e r e n c e r e p o s i t o r y = " r e p o n a m e " r e v i s i o n = " r e v " } } } } } } w h e r e t h e a r g u m e n t s a r e t h e f o l l o w i n g : - ` r e p o s i t o r y ` : t h e r e p o s i t o r y c o n t a i n i n g t h e c h a n g e s e t - ` r e v i s i o n ` : t h e r e v i s i o n o f t h e d e s i r e d c h a n g e s e t

[[Image]]

E m b e d a n i m a g e i n w i k i - f o r m a t t e d t e x t . T h e f i r s t a r g u m e n t i s t h e f i l e s p e c i f i c a t i o n . T h e f i l e s p e c i f i c a t i o n m a y r e f e r e n c e a t t a c h m e n t s i n t h r e e w a y s : * ` m o d u l e : i d : f i l e ` , w h e r e m o d u l e c a n b e e i t h e r ' ' ' w i k i ' ' ' o r ' ' ' t i c k e t ' ' ' , t o r e f e r t o t h e a t t a c h m e n t n a m e d ' ' f i l e ' ' o f t h e s p e c i f i e d w i k i p a g e o r t i c k e t . * ` i d : f i l e ` : s a m e a s a b o v e , b u t i d i s e i t h e r a t i c k e t s h o r t h a n d o r a W i k i p a g e n a m e . * ` f i l e ` t o r e f e r t o a l o c a l a t t a c h m e n t n a m e d ' f i l e ' . T h i s o n l y w o r k s f r o m w i t h i n t h a t w i k i p a g e o r a t i c k e t . A l s o , t h e f i l e s p e c i f i c a t i o n m a y r e f e r t o r e p o s i t o r y f i l e s , u s i n g t h e ` s o u r c e : f i l e ` s y n t a x ( ` s o u r c e : f i l e @ r e v ` w o r k s a l s o ) . F i l e s c a n a l s o b e a c c e s s e d w i t h a d i r e c t U R L s ; ` / f i l e ` f o r a p r o j e c t - r e l a t i v e , ` / / f i l e ` f o r a s e r v e r - r e l a t i v e , o r ` h t t p : / / s e r v e r / f i l e ` f o r a b s o l u t e l o c a t i o n o f t h e f i l e . T h e r e m a i n i n g a r g u m e n t s a r e o p t i o n a l a n d a l l o w c o n f i g u r i n g t h e a t t r i b u t e s a n d s t y l e o f t h e r e n d e r e d ` < i m g

` e l e m e n t : * d i g i t s a n d u n i t a r e i n t e r p r e t e d a s t h e s i z e ( e x . 1 2 0 , 2 5 % ) f o r t h e i m a g e * ` r i g h t ` , ` l e f t ` , ` c e n t e r ` , ` t o p ` , ` b o t t o m ` a n d ` m i d d l e ` a r e i n t e r p r e t e d a s t h e a l i g n m e n t f o r t h e i m a g e ( a l t e r n a t i v e l y , t h e f i r s t t h r e e c a n b e s p e c i f i e d u s i n g ` a l i g n = . . . ` a n d t h e l a s t t h r e e u s i n g ` v a l i g n = . . . ` ) * ` l i n k = s o m e T r a c L i n k s . . . ` r e p l a c e s t h e l i n k t o t h e i m a g e s o u r c e b y t h e o n e s p e c i f i e d u s i n g a T r a c L i n k s . I f n o v a l u e i s s p e c i f i e d , t h e l i n k i s s i m p l y r e m o v e d . * ` n o l i n k ` m e a n s w i t h o u t l i n k t o i m a g e s o u r c e ( d e p r e c a t e d , u s e ` l i n k = ` ) * ` k e y = v a l u e ` s t y l e a r e i n t e r p r e t e d a s H T M L a t t r i b u t e s o r C S S s t y l e i n d i c a t i o n s f o r t h e i m a g e . V a l i d k e y s a r e : * a l i g n , v a l i g n , b o r d e r , w i d t h , h e i g h t , a l t , t i t l e , l o n g d e s c , c l a s s , m a r g i n , m a r g i n - ( l e f t , r i g h t , t o p , b o t t o m ) , i d a n d u s e m a p * ` b o r d e r ` , ` m a r g i n ` , a n d ` m a r g i n - ` * c a n o n l y b e a s i n g l e n u m b e r * ` m a r g i n ` i s s u p e r s e d e d b y ` c e n t e r ` w h i c h u s e s a u t o m a r g i n s E x a m p l e s : { { { [ [ I m a g e ( p h o t o . j p g ) ] ] # s i m p l e s t [ [ I m a g e ( p h o t o . j p g , 1 2 0 p x ) ] ] # w i t h i m a g e w i d t h s i z e [ [ I m a g e ( p h o t o . j p g , r i g h t ) ] ] # a l i g n e d b y k e y w o r d [ [ I m a g e ( p h o t o . j p g , n o l i n k ) ] ] # w i t h o u t l i n k t o s o u r c e [ [ I m a g e ( p h o t o . j p g , a l i g n = r i g h t ) ] ] # a l i g n e d b y a t t r i b u t e } } } Y o u c a n u s e i m a g e f r o m o t h e r p a g e , o t h e r t i c k e t o r o t h e r m o d u l e . { { { [ [ I m a g e ( O t h e r P a g e : f o o . b m p ) ] ] # i f c u r r e n t m o d u l e i s w i k i [ [ I m a g e ( b a s e / s u b : b a r . b m p ) ] ] # f r o m h i e r a r c h i c a l w i k i p a g e [ [ I m a g e ( # 3 : b a z . b m p ) ] ] # i f i n a t i c k e t , p o i n t t o # 3 [ [ I m a g e ( t i c k e t : 3 6 : b o o . j p g ) ] ] [ [ I m a g e ( s o u r c e : / i m a g e s / b e e . j p g ) ] ] # s t r a i g h t f r o m t h e r e p o s i t o r y ! [ [ I m a g e ( h t d o c s : f o o / b a r . p n g ) ] ] # i m a g e f i l e i n p r o j e c t h t d o c s d i r . } } } ' ' A d a p t e d f r o m t h e I m a g e . p y m a c r o c r e a t e d b y S h u n - i c h i G o t o < g o t o h @ t a i y o . c o . j p

' '

[[InterTrac]]

P r o v i d e a l i s t o f k n o w n I n t e r T r a c p r e f i x e s .

[[InterWiki]]

P r o v i d e a d e s c r i p t i o n l i s t f o r t h e k n o w n I n t e r W i k i p r e f i x e s .

[[KnownMimeTypes]]

L i s t a l l k n o w n m i m e - t y p e s w h i c h c a n b e u s e d a s W i k i P r o c e s s o r s . C a n b e g i v e n a n o p t i o n a l a r g u m e n t w h i c h i s i n t e r p r e t e d a s m i m e - t y p e f i l t e r .

[[MacroList]]

D i s p l a y a l i s t o f a l l i n s t a l l e d W i k i m a c r o s , i n c l u d i n g d o c u m e n t a t i o n i f a v a i l a b l e . O p t i o n a l l y , t h e n a m e o f a s p e c i f i c m a c r o c a n b e p r o v i d e d a s a n a r g u m e n t . I n t h a t c a s e , o n l y t h e d o c u m e n t a t i o n f o r t h a t m a c r o w i l l b e r e n d e r e d . N o t e t h a t t h i s m a c r o w i l l n o t b e a b l e t o d i s p l a y t h e d o c u m e n t a t i o n o f m a c r o s i f t h e ` P y t h o n O p t i m i z e ` o p t i o n i s e n a b l e d f o r m o d _ p y t h o n !

[[PageOutline]]

D i s p l a y a s t r u c t u r a l o u t l i n e o f t h e c u r r e n t w i k i p a g e , e a c h i t e m i n t h e o u t l i n e b e i n g a l i n k t o t h e c o r r e s p o n d i n g h e a d i n g . T h i s m a c r o a c c e p t s f o u r o p t i o n a l p a r a m e t e r s : * T h e f i r s t i s a n u m b e r o r r a n g e t h a t a l l o w s c o n f i g u r i n g t h e m i n i m u m a n d m a x i m u m l e v e l o f h e a d i n g s t h a t s h o u l d b e i n c l u d e d i n t h e o u t l i n e . F o r e x a m p l e , s p e c i f y i n g " 1 " h e r e w i l l r e s u l t i n o n l y t h e t o p - l e v e l h e a d i n g s b e i n g i n c l u d e d i n t h e o u t l i n e . S p e c i f y i n g " 2 - 3 " w i l l m a k e t h e o u t l i n e i n c l u d e a l l h e a d i n g s o f l e v e l 2 a n d 3 , a s a n e s t e d l i s t . T h e d e f a u l t i s t o i n c l u d e a l l h e a d i n g l e v e l s . * T h e s e c o n d p a r a m e t e r c a n b e u s e d t o s p e c i f y a c u s t o m t i t l e ( t h e d e f a u l t i s n o t i t l e ) . * T h e t h i r d p a r a m e t e r s e l e c t s t h e s t y l e o f t h e o u t l i n e . T h i s c a n b e e i t h e r ` i n l i n e ` o r ` p u l l o u t ` ( t h e l a t t e r b e i n g t h e d e f a u l t ) . T h e ` i n l i n e ` s t y l e r e n d e r s t h e o u t l i n e a s n o r m a l p a r t o f t h e c o n t e n t , w h i l e ` p u l l o u t ` c a u s e s t h e o u t l i n e t o b e r e n d e r e d i n a b o x t h a t i s b y d e f a u l t f l o a t e d t o t h e r i g h t s i d e o f t h e o t h e r c o n t e n t . * T h e f o u r t h p a r a m e t e r s p e c i f i e s w h e t h e r t h e o u t l i n e i s n u m b e r e d o r n o t . I t c a n b e e i t h e r ` n u m b e r e d ` o r ` u n n u m b e r e d ` ( t h e f o r m e r b e i n g t h e d e f a u l t ) . T h i s p a r a m e t e r o n l y h a s a n e f f e c t i n ` i n l i n e ` s t y l e .

[[ProjectStats]]

Wiki macro listing some generic Trac statistics.

This macro accepts a comma-separated list of keyed parameters, in the form "key=value". Valid keys:

  • wiki -- statistics for TracWiki, values:
    • count -- show wiki page count
  • prefix -- use with wiki key: only names that start with that prefix are included

'count' is also recognized without prepended key name.

[[RecentChanges]]

L i s t a l l p a g e s t h a t h a v e r e c e n t l y b e e n m o d i f i e d , o r d e r e d b y t h e t i m e t h e y w e r e l a s t m o d i f i e d . T h i s m a c r o a c c e p t s t w o o r d e r e d a r g u m e n t s a n d a n a m e d a r g u m e n t . T h e n a m e d a r g u m e n t c a n b e p l a c e d i n a n y p o s i t i o n w i t h i n t h e a r g u m e n t l i s t . T h e f i r s t p a r a m e t e r i s a p r e f i x s t r i n g : i f p r o v i d e d , o n l y p a g e s w i t h n a m e s t h a t s t a r t w i t h t h e p r e f i x a r e i n c l u d e d i n t h e r e s u l t i n g l i s t . I f t h i s p a r a m e t e r i s o m i t t e d , a l l p a g e s a r e i n c l u d e d i n t h e l i s t . T h e s e c o n d p a r a m e t e r i s t h e m a x i m u m n u m b e r o f p a g e s t o i n c l u d e i n t h e l i s t . T h e ` g r o u p ` p a r a m e t e r d e t e r m i n e s h o w t h e l i s t i s p r e s e n t e d : ` g r o u p = d a t e ` : : T h e p a g e s a r e p r e s e n t e d i n b u l l e t e d l i s t s t h a t a r e g r o u p e d b y d a t e ( d e f a u l t ) . ` g r o u p = n o n e ` : : T h e p a g e s a r e p r e s e n t e d i n a s i n g l e b u l l e t e d l i s t . T i p : i f y o u o n l y w a n t t o s p e c i f y a m a x i m u m n u m b e r o f e n t r i e s a n d d o n ' t w a n t t o f i l t e r b y p r e f i x , s p e c i f y a n e m p t y f i r s t p a r a m e t e r , e . g . ` [ [ R e c e n t C h a n g e s ( , 1 0 , g r o u p = n o n e ) ] ] ` .

[[RepositoryIndex]]

D i s p l a y t h e l i s t o f a v a i l a b l e r e p o s i t o r i e s . C a n b e g i v e n t h e f o l l o w i n g n a m e d a r g u m e n t s : ' ' f o r m a t ' ' : : S e l e c t t h e r e n d e r i n g f o r m a t : - ' ' c o m p a c t ' ' p r o d u c e s a c o m m a - s e p a r a t e d l i s t o f r e p o s i t o r y p r e f i x n a m e s ( d e f a u l t ) - ' ' l i s t ' ' p r o d u c e s a d e s c r i p t i o n l i s t o f r e p o s i t o r y p r e f i x n a m e s - ' ' t a b l e ' ' p r o d u c e s a t a b l e v i e w , s i m i l a r t o t h e o n e v i s i b l e i n t h e ' ' B r o w s e V i e w ' ' p a g e ' ' g l o b ' ' : : D o a g l o b - s t y l e f i l t e r i n g o n t h e r e p o s i t o r y n a m e s ( d e f a u l t s t o ' * ' ) ' ' o r d e r ' ' : : O r d e r r e p o s i t o r i e s b y t h e g i v e n c o l u m n ( o n e o f " n a m e " , " d a t e " o r " a u t h o r " ) ' ' d e s c ' ' : : W h e n s e t t o 1 , o r d e r b y d e s c e n d i n g o r d e r ( ' ' s i n c e 0 . 1 2 ' ' )

[[TitleIndex]]

I n s e r t a n a l p h a b e t i c l i s t o f a l l w i k i p a g e s i n t o t h e o u t p u t . A c c e p t s a p r e f i x s t r i n g a s p a r a m e t e r : i f p r o v i d e d , o n l y p a g e s w i t h n a m e s t h a t s t a r t w i t h t h e p r e f i x a r e i n c l u d e d i n t h e r e s u l t i n g l i s t . I f t h i s p a r a m e t e r i s o m i t t e d , a l l p a g e s a r e l i s t e d . I f t h e p r e f i x i s s p e c i f i e d , a s e c o n d a r g u m e n t o f v a l u e ` h i d e p r e f i x ` c a n b e g i v e n a s w e l l , i n o r d e r t o r e m o v e t h a t p r e f i x f r o m t h e o u t p u t . A l t e r n a t e ` f o r m a t ` a n d ` d e p t h ` n a m e d p a r a m e t e r s c a n b e s p e c i f i e d : - ` f o r m a t = c o m p a c t ` : T h e p a g e s a r e d i s p l a y e d a s c o m m a - s e p a r a t e d l i n k s . - ` f o r m a t = g r o u p ` : T h e l i s t o f p a g e s w i l l b e s t r u c t u r e d i n g r o u p s a c c o r d i n g t o c o m m o n p r e f i x . T h i s f o r m a t a l s o s u p p o r t s a ` m i n = n ` a r g u m e n t , w h e r e ` n ` i s t h e m i n i m a l n u m b e r o f p a g e s f o r a g r o u p . - ` f o r m a t = h i e r a r c h y ` : T h e l i s t o f p a g e s w i l l b e s t r u c t u r e d a c c o r d i n g t o t h e p a g e n a m e p a t h h i e r a r c h y . T h i s f o r m a t a l s o s u p p o r t s a ` m i n = n ` a r g u m e n t , w h e r e h i g h e r ` n ` f l a t t e n t h e d i s p l a y h i e r a r c h y - ` d e p t h = n ` : l i m i t t h e d e p t h o f t h e p a g e s t o l i s t . I f s e t t o 0 , o n l y t o p l e v e l p a g e s w i l l b e s h o w n , i f s e t t o 1 , o n l y i m m e d i a t e c h i l d r e n p a g e s w i l l b e s h o w n , e t c . I f n o t s e t , o r s e t t o - 1 , a l l p a g e s i n t h e h i e r a r c h y w i l l b e s h o w n . - ` i n c l u d e = p a g e 1 : p a g e * 2 ` : i n c l u d e o n l y p a g e s t h a t m a t c h a n i t e m i n t h e c o l o n - s e p a r a t e d l i s t o f p a g e s . I f t h e l i s t i s e m p t y , o r i f n o ` i n c l u d e ` a r g u m e n t i s g i v e n , i n c l u d e a l l p a g e s . - ` e x c l u d e = p a g e 1 : p a g e * 2 ` : e x c l u d e p a g e s t h a t m a t c h a n i t e m i n t h e c o l o n - s e p a r a t e d l i s t o f p a g e s . T h e ` i n c l u d e ` a n d ` e x c l u d e ` l i s t s a c c e p t s h e l l - s t y l e p a t t e r n s .

[[TracAdminHelp]]

D i s p l a y h e l p f o r t r a c - a d m i n c o m m a n d s . E x a m p l e s : { { { [ [ T r a c A d m i n H e l p ] ] # a l l c o m m a n d s [ [ T r a c A d m i n H e l p ( w i k i ) ] ] # a l l w i k i c o m m a n d s [ [ T r a c A d m i n H e l p ( w i k i e x p o r t ) ] ] # t h e " w i k i e x p o r t " c o m m a n d [ [ T r a c A d m i n H e l p ( u p g r a d e ) ] ] # t h e u p g r a d e c o m m a n d } } }

[[TracGuideToc]]

D i s p l a y a t a b l e o f c o n t e n t f o r t h e T r a c g u i d e . T h i s m a c r o s h o w s a q u i c k a n d d i r t y w a y t o m a k e a t a b l e - o f - c o n t e n t s f o r t h e H e l p / G u i d e . T h e t a b l e o f c o n t e n t s w i l l c o n t a i n t h e T r a c * a n d W i k i F o r m a t t i n g p a g e s , a n d c a n ' t b e c u s t o m i z e d . S e a r c h f o r T o c M a c r o f o r a a m o r e c u s t o m i z a b l e t a b l e o f c o n t e n t s .

[[TracIni]]

P r o d u c e d o c u m e n t a t i o n f o r t h e T r a c c o n f i g u r a t i o n f i l e . T y p i c a l l y , t h i s w i l l b e u s e d i n t h e T r a c I n i p a g e . O p t i o n a l a r g u m e n t s a r e a c o n f i g u r a t i o n s e c t i o n f i l t e r , a n d a c o n f i g u r a t i o n o p t i o n n a m e f i l t e r : o n l y t h e c o n f i g u r a t i o n o p t i o n s w h o s e s e c t i o n a n d n a m e s t a r t w i t h t h e f i l t e r s a r e o u t p u t .

[[UserQuery]]

Wiki macro listing users that match certain criteria.

This macro accepts a comma-separated list of keyed parameters, in the form "key=value". Valid keys:

  • perm -- show only that users, a permission action given by value has been granted to
  • locked -- retrieve users, who's account has/has not been locked depending on boolean value
  • format -- output style: 'count', 'list' or comma-separated values (default)
  • nomatch -- replacement wiki markup that is displayed, if there's no match and output style isn't 'count' either

'count' is also recognized without prepended key name. Other non-keyed parameters are:

  • locked -- alias for 'locked=True'
  • visit -- show a list of accounts with last-login information, only available in table format
  • name -- forces replacement of maching username with their corresponding full names, if available; adds a full names column if combined with 'visit'
  • email -- append email address to usernames, if available

Requires USER_VIEW permission for output in any format other then 'count'. A misc placeholder with this statement is presented to unprivileged users.

Macros from around the world

The Trac Hacks site provides a wide collection of macros and other Trac plugins contributed by the Trac community. If you're looking for new macros, or have written one that you'd like to share with the world, please don't hesitate to visit that site.

Developing Custom Macros

Macros, like Trac itself, are written in the Python programming language.

For more information about developing macros, see the development resources on the main project site.

Implementation

Here are 2 simple examples showing how to create a Macro with Trac 0.11.

Also, have a look at Timestamp.py for an example that shows the difference between old style and new style macros and at the macros/README which provides a little more insight about the transition.

Macro without arguments

It should be saved as TimeStamp.py as Trac will use the module name as the Macro name

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

class TimeStampMacro(WikiMacroBase):
    """Inserts the current time (in seconds) into the wiki page."""

    revision = "$Rev$"
    url = "$URL$"

    def expand_macro(self, formatter, name, args):
        t = datetime.now(utc)
        return tag.b(format_datetime(t, '%c'))

Macro with arguments

It should be saved as HelloWorld.py (in the plugins/ directory) as Trac will use the module name as the Macro name

from trac.wiki.macros import WikiMacroBase

class HelloWorldMacro(WikiMacroBase):
    """Simple HelloWorld macro.

    Note that the name of the class is meaningful:
     - it must end with "Macro"
     - what comes before "Macro" ends up being the macro name

    The documentation of the class (i.e. what you're reading)
    will become the documentation of the macro, as shown by
    the !MacroList macro (usually used in the WikiMacros page).
    """

    revision = "$Rev$"
    url = "$URL$"

    def expand_macro(self, formatter, name, args):
        """Return some output that will be displayed in the Wiki content.

        `name` is the actual name of the macro (no surprise, here it'll be
        `'HelloWorld'`),
        `args` is the text enclosed in parenthesis at the call of the macro.
          Note that if there are ''no'' parenthesis (like in, e.g.
          [[HelloWorld]]), then `args` is `None`.
        """
        return 'Hello World, args = ' + unicode(args)
    
    # Note that there's no need to HTML escape the returned data,
    # as the template engine (Genshi) will do it for us.

expand_macro details

expand_macro should return either a simple Python string which will be interpreted as HTML, or preferably a Markup object (use from trac.util.html import Markup). Markup(string) just annotates the string so the renderer will render the HTML string as-is with no escaping. You will also need to import Formatter using from trac.wiki import Formatter.

If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:

  text = "whatever wiki markup you want, even containing other macros"
  # Convert Wiki markup to HTML, new style
  out = StringIO()
  Formatter(self.env, formatter.context).format(text, out)
  return Markup(out.getvalue())