hgbook
diff en/template.tex @ 80:ea951cfb5cd9
Much template-related content.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Wed Sep 06 23:36:47 2006 -0700 (2006-09-06) |
parents | df88df78288d |
children | b476081a9c04 |
line diff
1.1 --- a/en/template.tex Mon Sep 04 10:58:09 2006 -0700 1.2 +++ b/en/template.tex Wed Sep 06 23:36:47 2006 -0700 1.3 @@ -1,5 +1,288 @@ 1.4 -\chapter{Using templates to customise output} 1.5 -\label{chap:templates} 1.6 +\chapter{Customising the output of Mercurial} 1.7 +\label{chap:template} 1.8 + 1.9 +Mercurial provides a powerful mechanism to let you control how it 1.10 +displays information. The mechanism is based on templates. You can 1.11 +use templates to generate specific output for a single command, or to 1.12 +customise the entire appearance of the built-in web interface. 1.13 + 1.14 +\section{Using precanned output styles} 1.15 +\label{sec:style} 1.16 + 1.17 +Packaged with Mercurial are some output styles that you can use 1.18 +immediately. A style is simply a precanned template that someone 1.19 +wrote. 1.20 + 1.21 +Before we take a look at Mercurial's bundled styles, let's review its 1.22 +normal output. 1.23 + 1.24 +\interaction{template.simple.normal} 1.25 + 1.26 +This is somewhat informative, but it takes up a lot of space---five 1.27 +lines of output per changeset. The \texttt{compact} style reduces 1.28 +this to three lines, presented in a sparse manner. 1.29 + 1.30 +\interaction{template.simple.compact} 1.31 + 1.32 +The \texttt{changelog} style hints at the expressive power of 1.33 +Mercurial's templating engine. This style attempts to follow the GNU 1.34 +Project's changelog guidelines\cite{web:changelog}. 1.35 + 1.36 +\interaction{template.simple.changelog} 1.37 + 1.38 +You will not be shocked to learn that Mercurial's default output style 1.39 +is named \texttt{default}. 1.40 + 1.41 +\subsection{Setting an output style to use} 1.42 + 1.43 +You can modify the output style that Mercurial will use for every 1.44 +command by editing your \hgrc\ file, naming the style you would 1.45 +prefer to use. 1.46 + 1.47 +\begin{codesample2} 1.48 + [ui] 1.49 + style = compact 1.50 +\end{codesample2} 1.51 + 1.52 +If you write a style of your own, you can use it by either providing 1.53 +the path to your style file, or copying your style file into a 1.54 +location where Mercurial can find it (typically the \texttt{templates} 1.55 +subdirectory of your Mercurial install directory). 1.56 + 1.57 +\section{Commands that support styles and templates} 1.58 + 1.59 +All of Mercurial's ``\texttt{log}-like'' commands let you use styles 1.60 +and templates: \hgcmd{incoming}, \hgcmd{log}, \hgcmd{outgoing}, and 1.61 +\hgcmd{tip}. 1.62 + 1.63 +As I write this manual, these are so far the only commands that 1.64 +support styles and templates. Since these are the most important 1.65 +commands that need customisable output, there has been little pressure 1.66 +from the Mercurial user community to add style and template support to 1.67 +other commands. 1.68 + 1.69 +\section{The basics of templating} 1.70 + 1.71 +At its simplest, a Mercurial template is a piece of text. Some of the 1.72 +text never changes, while other parts are \emph{expanded}, or replaced 1.73 +with new text, when necessary. 1.74 + 1.75 +Before we continue, let's look again at a simple example of 1.76 +Mercurial's normal output. 1.77 + 1.78 +\interaction{template.simple.normal} 1.79 + 1.80 +Now, let's run the same command, but using a template to change its 1.81 +output. 1.82 + 1.83 +\interaction{template.simple.simplest} 1.84 + 1.85 +The example above illustrates the simplest possible template; it's 1.86 +just a piece of static text, printed once for each changeset. The 1.87 +\hgopt{log}{--template} option to the \hgcmd{log} command tells 1.88 +Mercurial to use the given text as the template when printing each 1.89 +changeset. 1.90 + 1.91 +Notice that the template string above ends with the text 1.92 +``\Verb+\n+''. This is an \emph{escape sequence}, telling Mercurial 1.93 +to print a newline at the end of each template item. If you omit this 1.94 +newline, Mercurial will run each piece of output together. See 1.95 +section~\ref{sec:template:escape} for more details of escape sequences. 1.96 + 1.97 +A template that prints a fixed string of text all the time isn't very 1.98 +useful; let's try something a bit more complex. 1.99 + 1.100 +\interaction{template.simple.simplesub} 1.101 + 1.102 +As you can see, the string ``\Verb+{desc}+'' in the template has been 1.103 +replaced in the output with the description of each changeset. Every 1.104 +time Mercurial finds text enclosed in curly braces (``\texttt{\{}'' 1.105 +and ``\texttt{\}}''), it will try to replace the braces and text with 1.106 +the expansion of whatever is inside. To print a literal curly brace, 1.107 +you must escape it, as described in section~\ref{sec:template:escape}. 1.108 + 1.109 +\section{Basic template keywords} 1.110 +\label{sec:template:keyword} 1.111 + 1.112 +You can start writing simple templates immediately using the keywords 1.113 +below. 1.114 + 1.115 +\begin{itemize} 1.116 +\item[\tplkword{author}] String. The unmodified author of the changeset. 1.117 +\item[\tplkword{date}] Date information. The date when the changeset 1.118 + was committed. This is \emph{not} human-readable; you must pass it 1.119 + through a filter that will render it appropriately. See 1.120 + section~\ref{sec:template:filter} for more information on filters. 1.121 + The date is expressed as a pair of numbers. The first number is a 1.122 + Unix UTC timestamp (seconds since January 1, 1970); the second is 1.123 + the offset of the committer's timezone from UTC, in seconds. 1.124 +\item[\tplkword{desc}] String. The text of the changeset description. 1.125 +\item[\tplkword{files}] List of strings. All files modified, added, or 1.126 + removed by this changeset. 1.127 +\item[\tplkword{file\_adds}] List of strings. Files added by this 1.128 + changeset. 1.129 +\item[\tplkword{file\_dels}] List of strings. Files removed by this 1.130 + changeset. 1.131 +\item[\tplkword{node}] String. The changeset identification hash, as a 1.132 + 40-character hexadecimal string. 1.133 +\item[\tplkword{parents}] List of strings. The parents of the 1.134 + changeset. 1.135 +\item[\tplkword{rev}] Integer. The repository-local changeset revision 1.136 + number. 1.137 +\item[\tplkword{tags}] List of strings. Any tags associated with the 1.138 + changeset. 1.139 +\end{itemize} 1.140 + 1.141 +A few simple experiments will show us what to expect when we use these 1.142 +keywords; you can see the results in 1.143 +figure~\ref{fig:template:keywords}. 1.144 + 1.145 +\begin{figure} 1.146 + \interaction{template.simple.keywords} 1.147 + \caption{Template keywords in use} 1.148 + \label{fig:template:keywords} 1.149 +\end{figure} 1.150 + 1.151 +As we noted above, the date keyword does not produce human-readable 1.152 +output, so we must treat it specially. This involves using a 1.153 +\emph{filter}, about which more in section~\ref{sec:template:filter}. 1.154 + 1.155 +\interaction{template.simple.datekeyword} 1.156 + 1.157 +\section{Escape sequences} 1.158 +\label{sec:template:escape} 1.159 + 1.160 +Mercurial's templating engine recognises the most commonly used escape 1.161 +sequences in strings. When it sees a backslash (``\Verb+\+'') 1.162 +character, it looks at the following character and substitutes the two 1.163 +characters with a single replacement, as described below. 1.164 + 1.165 +\begin{itemize} 1.166 +\item[\Verb+\textbackslash\textbackslash+] Backslash, ``\Verb+\+'', 1.167 + ASCII~134. 1.168 +\item[\Verb+\textbackslash n+] Newline, ASCII~12. 1.169 +\item[\Verb+\textbackslash r+] Carriage return, ASCII~15. 1.170 +\item[\Verb+\textbackslash t+] Tab, ASCII~11. 1.171 +\item[\Verb+\textbackslash v+] Vertical tab, ASCII~13. 1.172 +\item[\Verb+\textbackslash \{+] Open curly brace, ``\Verb+{+'', ASCII~173. 1.173 +\item[\Verb+\textbackslash \}+] Close curly brace, ``\Verb+}+'', ASCII~175. 1.174 +\end{itemize} 1.175 + 1.176 +As indicated above, if you want the expansion of a template to contain 1.177 +a literal ``\Verb+\+'', ``\Verb+{+'', or ``\Verb+{+'' character, you 1.178 +must escape it. 1.179 + 1.180 +\section{Filtering expanded keywords} 1.181 +\label{sec:template:filter} 1.182 + 1.183 +Some of the results of template expansion are not entirely easy to 1.184 +use. Mercurial lets you specify an optional chain of \emph{filters} 1.185 +to modify the result of expanding a keyword. You have already seen a 1.186 +common filter, \tplkwfilt{date}{isodate}, in action above, to make a 1.187 +date readable. 1.188 + 1.189 +\begin{itemize} 1.190 +\item[\tplfilter{addbreaks}] Any text. Add an XHTML ``\Verb+<br/>+'' 1.191 + tag before the end of every line except the last. For example, 1.192 + ``\Verb+foo\nbar+'' becomes ``\Verb+foo<br/>\nbar+''. 1.193 +\item[\tplkwfilt{date}{age}] \tplkword{date} keyword. Render the 1.194 + age of the date, relative to the current time. Yields a string like 1.195 + ``\Verb+10 minutes+''. 1.196 +\item[\tplfilter{basename}] Any text, but most useful for the 1.197 + \tplkword{files} keyword and its relatives. Treat the text as a 1.198 + path, and return the basename. For example, ``\Verb+foo/bar/baz+'' 1.199 + becomes ``\Verb+baz+''. 1.200 +\item[\tplkwfilt{date}{date}] \tplkword{date} keyword. Render a date 1.201 + in a similar format to the Unix \tplkword{date} command, but with 1.202 + timezone included. Yields a string like 1.203 + ``\Verb+Mon Sep 04 15:13:13 2006 -0700+''. 1.204 +\item[\tplkwfilt{author}{domain}] Any text, but most useful for the 1.205 + \tplkword{author} keyword. Finds the first string that looks like 1.206 + an email address, and extract just the domain component. For 1.207 + example, ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' becomes 1.208 + ``\Verb+serpentine.com+''. 1.209 +\item[\tplkwfilt{author}{email}] Any text, but most useful for the 1.210 + \tplkword{author} keyword. Extract the first string that looks like 1.211 + an email address. For example, 1.212 + ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' becomes 1.213 + ``\Verb+bos@serpentine.com+''. 1.214 +\item[\tplfilter{escape}] Any text. Replace the special XML/XHTML 1.215 + characters ``\Verb+&+'', ``\Verb+<+'' and ``\Verb+>+'' with 1.216 + XML entities. 1.217 +\item[\tplfilter{fill68}] Any text. Wrap the text to fit in 68 1.218 + columns. This is useful before you pass text through the 1.219 + \tplfilter{tabindent} filter, and still want it to fit in an 1.220 + 80-column fixed-font window. 1.221 +\item[\tplfilter{fill76}] Any text. Wrap the text to fit in 76 1.222 + columns. 1.223 +\item[\tplfilter{firstline}] Any text. Yield the first line of text, 1.224 + without any trailing newlines. 1.225 +\item[\tplkwfilt{date}{hgdate}] \tplkword{date} keyword. Render the 1.226 + date as a pair of readable numbers. Yields a string like 1.227 + ``\Verb+1157407993 25200+''. 1.228 +\item[\tplkwfilt{date}{isodate}] \tplkword{date} keyword. Render the 1.229 + date as a text string in ISO~8601 format. Yields a string like 1.230 + ``\Verb+2006-09-04 15:13:13 -0700+''. 1.231 +\item[\tplfilter{obfuscate}] Any text, but most useful for the 1.232 + \tplkword{author} keyword. Yield the input text rendered as a 1.233 + sequence of XML entities. This helps to defeat some particularly 1.234 + stupid screen-scraping email harvesting spambots. 1.235 +\item[\tplkwfilt{author}{person}] Any text, but most useful for the 1.236 + \tplkword{author} keyword. Yield the text before an email address. 1.237 + For example, ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' 1.238 + becomes ``\Verb+Bryan O'Sullivan+''. 1.239 +\item[\tplkwfilt{date}{rfc822date}] \tplkword{date} keyword. Render a 1.240 + date using the same format used in email headers. Yields a string 1.241 + like ``\Verb+Mon, 04 Sep 2006 15:13:13 -0700+''. 1.242 +\item[\tplkwfilt{node}{short}] Changeset hash. Yield the short form 1.243 + of a changeset hash, i.e.~a 12-byte hexadecimal string. 1.244 +\item[\tplkwfilt{date}{shortdate}] \tplkword{date} keyword. Render 1.245 + the year, month, and day of the date. Yields a string like 1.246 + ``\Verb+2006-09-04+''. 1.247 +\item[\tplfilter{strip}] Any text. Strip all leading and trailing 1.248 + whitespace from the string. 1.249 +\item[\tplfilter{tabindent}] Any text. Yield the text, with every line 1.250 + except the first starting with a tab character. 1.251 +\item[\tplfilter{urlescape}] Any text. Escape all characters that are 1.252 + considered ``special'' by URL parsers. For example, \Verb+foo bar+ 1.253 + becomes \Verb+foo%20bar+. 1.254 +\item[\tplkwfilt{author}{user}] Any text, but most useful for the 1.255 + \tplkword{author} keyword. Return the ``user'' portion of an email 1.256 + address. For example, 1.257 + ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' becomes 1.258 + ``\Verb+bos+''. 1.259 +\end{itemize} 1.260 + 1.261 +\begin{figure} 1.262 + \interaction{template.simple.manyfilters} 1.263 + \caption{Template filters in action} 1.264 + \label{fig:template:filters} 1.265 +\end{figure} 1.266 + 1.267 +\begin{note} 1.268 + If you try to apply a filter to a piece of data that it cannot 1.269 + process, Mercurial will fail and print a Python exception. For 1.270 + example, trying to run the output of the \tplkword{desc} keyword 1.271 + into the \tplkwfilt{date}{isodate} filter is not a good idea. 1.272 +\end{note} 1.273 + 1.274 +\subsection{Combining filters} 1.275 + 1.276 +It is easy to combine filters to yield output in the form you would 1.277 +like. The following chain of filters tidies up a description, then 1.278 +makes sure that it fits cleanly into 68 columns, then indents it by a 1.279 +further 8~characters (at least on Unix-like systems, where a tab is 1.280 +conventionally 8~characters wide). 1.281 + 1.282 +\interaction{template.simple.combine} 1.283 + 1.284 +Note the use of ``\Verb+\t+'' (a tab character) in the template to 1.285 +force the first line to be indented; this is necessary since 1.286 +\tplkword{tabindent} indents all lines \emph{except} the first. 1.287 + 1.288 +Keep in mind that the order of filters in a chain is significant. 1.289 +Using \Verb+fill68|tabindent+ gives very different results from 1.290 +\Verb+tabindent|fill68+. 1.291 1.292 %%% Local Variables: 1.293 %%% mode: latex