hgbook
diff es/template.tex @ 413:adccb58dbe9b
merging jerojasro changes
author | Igor TAmara <igor@tamarapatino.org> |
---|---|
date | Mon Nov 10 23:39:24 2008 -0500 (2008-11-10) |
parents | 04c08ad7e92e |
children | 012631b248de |
line diff
1.1 --- a/es/template.tex Sat Oct 18 07:48:21 2008 -0500 1.2 +++ b/es/template.tex Mon Nov 10 23:39:24 2008 -0500 1.3 @@ -0,0 +1,475 @@ 1.4 +\chapter{Customising the output of Mercurial} 1.5 +\label{chap:template} 1.6 + 1.7 +Mercurial provides a powerful mechanism to let you control how it 1.8 +displays information. The mechanism is based on templates. You can 1.9 +use templates to generate specific output for a single command, or to 1.10 +customise the entire appearance of the built-in web interface. 1.11 + 1.12 +\section{Using precanned output styles} 1.13 +\label{sec:style} 1.14 + 1.15 +Packaged with Mercurial are some output styles that you can use 1.16 +immediately. A style is simply a precanned template that someone 1.17 +wrote and installed somewhere that Mercurial can find. 1.18 + 1.19 +Before we take a look at Mercurial's bundled styles, let's review its 1.20 +normal output. 1.21 + 1.22 +\interaction{template.simple.normal} 1.23 + 1.24 +This is somewhat informative, but it takes up a lot of space---five 1.25 +lines of output per changeset. The \texttt{compact} style reduces 1.26 +this to three lines, presented in a sparse manner. 1.27 + 1.28 +\interaction{template.simple.compact} 1.29 + 1.30 +The \texttt{changelog} style hints at the expressive power of 1.31 +Mercurial's templating engine. This style attempts to follow the GNU 1.32 +Project's changelog guidelines\cite{web:changelog}. 1.33 + 1.34 +\interaction{template.simple.changelog} 1.35 + 1.36 +You will not be shocked to learn that Mercurial's default output style 1.37 +is named \texttt{default}. 1.38 + 1.39 +\subsection{Setting a default style} 1.40 + 1.41 +You can modify the output style that Mercurial will use for every 1.42 +command by editing your \hgrc\ file, naming the style you would 1.43 +prefer to use. 1.44 + 1.45 +\begin{codesample2} 1.46 + [ui] 1.47 + style = compact 1.48 +\end{codesample2} 1.49 + 1.50 +If you write a style of your own, you can use it by either providing 1.51 +the path to your style file, or copying your style file into a 1.52 +location where Mercurial can find it (typically the \texttt{templates} 1.53 +subdirectory of your Mercurial install directory). 1.54 + 1.55 +\section{Commands that support styles and templates} 1.56 + 1.57 +All of Mercurial's ``\texttt{log}-like'' commands let you use styles 1.58 +and templates: \hgcmd{incoming}, \hgcmd{log}, \hgcmd{outgoing}, and 1.59 +\hgcmd{tip}. 1.60 + 1.61 +As I write this manual, these are so far the only commands that 1.62 +support styles and templates. Since these are the most important 1.63 +commands that need customisable output, there has been little pressure 1.64 +from the Mercurial user community to add style and template support to 1.65 +other commands. 1.66 + 1.67 +\section{The basics of templating} 1.68 + 1.69 +At its simplest, a Mercurial template is a piece of text. Some of the 1.70 +text never changes, while other parts are \emph{expanded}, or replaced 1.71 +with new text, when necessary. 1.72 + 1.73 +Before we continue, let's look again at a simple example of 1.74 +Mercurial's normal output. 1.75 + 1.76 +\interaction{template.simple.normal} 1.77 + 1.78 +Now, let's run the same command, but using a template to change its 1.79 +output. 1.80 + 1.81 +\interaction{template.simple.simplest} 1.82 + 1.83 +The example above illustrates the simplest possible template; it's 1.84 +just a piece of static text, printed once for each changeset. The 1.85 +\hgopt{log}{--template} option to the \hgcmd{log} command tells 1.86 +Mercurial to use the given text as the template when printing each 1.87 +changeset. 1.88 + 1.89 +Notice that the template string above ends with the text 1.90 +``\Verb+\n+''. This is an \emph{escape sequence}, telling Mercurial 1.91 +to print a newline at the end of each template item. If you omit this 1.92 +newline, Mercurial will run each piece of output together. See 1.93 +section~\ref{sec:template:escape} for more details of escape sequences. 1.94 + 1.95 +A template that prints a fixed string of text all the time isn't very 1.96 +useful; let's try something a bit more complex. 1.97 + 1.98 +\interaction{template.simple.simplesub} 1.99 + 1.100 +As you can see, the string ``\Verb+{desc}+'' in the template has been 1.101 +replaced in the output with the description of each changeset. Every 1.102 +time Mercurial finds text enclosed in curly braces (``\texttt{\{}'' 1.103 +and ``\texttt{\}}''), it will try to replace the braces and text with 1.104 +the expansion of whatever is inside. To print a literal curly brace, 1.105 +you must escape it, as described in section~\ref{sec:template:escape}. 1.106 + 1.107 +\section{Common template keywords} 1.108 +\label{sec:template:keyword} 1.109 + 1.110 +You can start writing simple templates immediately using the keywords 1.111 +below. 1.112 + 1.113 +\begin{itemize} 1.114 +\item[\tplkword{author}] String. The unmodified author of the changeset. 1.115 +\item[\tplkword{branches}] String. The name of the branch on which 1.116 + the changeset was committed. Will be empty if the branch name was 1.117 + \texttt{default}. 1.118 +\item[\tplkword{date}] Date information. The date when the changeset 1.119 + was committed. This is \emph{not} human-readable; you must pass it 1.120 + through a filter that will render it appropriately. See 1.121 + section~\ref{sec:template:filter} for more information on filters. 1.122 + The date is expressed as a pair of numbers. The first number is a 1.123 + Unix UTC timestamp (seconds since January 1, 1970); the second is 1.124 + the offset of the committer's timezone from UTC, in seconds. 1.125 +\item[\tplkword{desc}] String. The text of the changeset description. 1.126 +\item[\tplkword{files}] List of strings. All files modified, added, or 1.127 + removed by this changeset. 1.128 +\item[\tplkword{file\_adds}] List of strings. Files added by this 1.129 + changeset. 1.130 +\item[\tplkword{file\_dels}] List of strings. Files removed by this 1.131 + changeset. 1.132 +\item[\tplkword{node}] String. The changeset identification hash, as a 1.133 + 40-character hexadecimal string. 1.134 +\item[\tplkword{parents}] List of strings. The parents of the 1.135 + changeset. 1.136 +\item[\tplkword{rev}] Integer. The repository-local changeset revision 1.137 + number. 1.138 +\item[\tplkword{tags}] List of strings. Any tags associated with the 1.139 + changeset. 1.140 +\end{itemize} 1.141 + 1.142 +A few simple experiments will show us what to expect when we use these 1.143 +keywords; you can see the results in 1.144 +figure~\ref{fig:template:keywords}. 1.145 + 1.146 +\begin{figure} 1.147 + \interaction{template.simple.keywords} 1.148 + \caption{Template keywords in use} 1.149 + \label{fig:template:keywords} 1.150 +\end{figure} 1.151 + 1.152 +As we noted above, the date keyword does not produce human-readable 1.153 +output, so we must treat it specially. This involves using a 1.154 +\emph{filter}, about which more in section~\ref{sec:template:filter}. 1.155 + 1.156 +\interaction{template.simple.datekeyword} 1.157 + 1.158 +\section{Escape sequences} 1.159 +\label{sec:template:escape} 1.160 + 1.161 +Mercurial's templating engine recognises the most commonly used escape 1.162 +sequences in strings. When it sees a backslash (``\Verb+\+'') 1.163 +character, it looks at the following character and substitutes the two 1.164 +characters with a single replacement, as described below. 1.165 + 1.166 +\begin{itemize} 1.167 +\item[\Verb+\textbackslash\textbackslash+] Backslash, ``\Verb+\+'', 1.168 + ASCII~134. 1.169 +\item[\Verb+\textbackslash n+] Newline, ASCII~12. 1.170 +\item[\Verb+\textbackslash r+] Carriage return, ASCII~15. 1.171 +\item[\Verb+\textbackslash t+] Tab, ASCII~11. 1.172 +\item[\Verb+\textbackslash v+] Vertical tab, ASCII~13. 1.173 +\item[\Verb+\textbackslash \{+] Open curly brace, ``\Verb+{+'', ASCII~173. 1.174 +\item[\Verb+\textbackslash \}+] Close curly brace, ``\Verb+}+'', ASCII~175. 1.175 +\end{itemize} 1.176 + 1.177 +As indicated above, if you want the expansion of a template to contain 1.178 +a literal ``\Verb+\+'', ``\Verb+{+'', or ``\Verb+{+'' character, you 1.179 +must escape it. 1.180 + 1.181 +\section{Filtering keywords to change their results} 1.182 +\label{sec:template:filter} 1.183 + 1.184 +Some of the results of template expansion are not immediately easy to 1.185 +use. Mercurial lets you specify an optional chain of \emph{filters} 1.186 +to modify the result of expanding a keyword. You have already seen a 1.187 +common filter, \tplkwfilt{date}{isodate}, in action above, to make a 1.188 +date readable. 1.189 + 1.190 +Below is a list of the most commonly used filters that Mercurial 1.191 +supports. While some filters can be applied to any text, others can 1.192 +only be used in specific circumstances. The name of each filter is 1.193 +followed first by an indication of where it can be used, then a 1.194 +description of its effect. 1.195 + 1.196 +\begin{itemize} 1.197 +\item[\tplfilter{addbreaks}] Any text. Add an XHTML ``\Verb+<br/>+'' 1.198 + tag before the end of every line except the last. For example, 1.199 + ``\Verb+foo\nbar+'' becomes ``\Verb+foo<br/>\nbar+''. 1.200 +\item[\tplkwfilt{date}{age}] \tplkword{date} keyword. Render the 1.201 + age of the date, relative to the current time. Yields a string like 1.202 + ``\Verb+10 minutes+''. 1.203 +\item[\tplfilter{basename}] Any text, but most useful for the 1.204 + \tplkword{files} keyword and its relatives. Treat the text as a 1.205 + path, and return the basename. For example, ``\Verb+foo/bar/baz+'' 1.206 + becomes ``\Verb+baz+''. 1.207 +\item[\tplkwfilt{date}{date}] \tplkword{date} keyword. Render a date 1.208 + in a similar format to the Unix \tplkword{date} command, but with 1.209 + timezone included. Yields a string like 1.210 + ``\Verb+Mon Sep 04 15:13:13 2006 -0700+''. 1.211 +\item[\tplkwfilt{author}{domain}] Any text, but most useful for the 1.212 + \tplkword{author} keyword. Finds the first string that looks like 1.213 + an email address, and extract just the domain component. For 1.214 + example, ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' becomes 1.215 + ``\Verb+serpentine.com+''. 1.216 +\item[\tplkwfilt{author}{email}] Any text, but most useful for the 1.217 + \tplkword{author} keyword. Extract the first string that looks like 1.218 + an email address. For example, 1.219 + ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' becomes 1.220 + ``\Verb+bos@serpentine.com+''. 1.221 +\item[\tplfilter{escape}] Any text. Replace the special XML/XHTML 1.222 + characters ``\Verb+&+'', ``\Verb+<+'' and ``\Verb+>+'' with 1.223 + XML entities. 1.224 +\item[\tplfilter{fill68}] Any text. Wrap the text to fit in 68 1.225 + columns. This is useful before you pass text through the 1.226 + \tplfilter{tabindent} filter, and still want it to fit in an 1.227 + 80-column fixed-font window. 1.228 +\item[\tplfilter{fill76}] Any text. Wrap the text to fit in 76 1.229 + columns. 1.230 +\item[\tplfilter{firstline}] Any text. Yield the first line of text, 1.231 + without any trailing newlines. 1.232 +\item[\tplkwfilt{date}{hgdate}] \tplkword{date} keyword. Render the 1.233 + date as a pair of readable numbers. Yields a string like 1.234 + ``\Verb+1157407993 25200+''. 1.235 +\item[\tplkwfilt{date}{isodate}] \tplkword{date} keyword. Render the 1.236 + date as a text string in ISO~8601 format. Yields a string like 1.237 + ``\Verb+2006-09-04 15:13:13 -0700+''. 1.238 +\item[\tplfilter{obfuscate}] Any text, but most useful for the 1.239 + \tplkword{author} keyword. Yield the input text rendered as a 1.240 + sequence of XML entities. This helps to defeat some particularly 1.241 + stupid screen-scraping email harvesting spambots. 1.242 +\item[\tplkwfilt{author}{person}] Any text, but most useful for the 1.243 + \tplkword{author} keyword. Yield the text before an email address. 1.244 + For example, ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' 1.245 + becomes ``\Verb+Bryan O'Sullivan+''. 1.246 +\item[\tplkwfilt{date}{rfc822date}] \tplkword{date} keyword. Render a 1.247 + date using the same format used in email headers. Yields a string 1.248 + like ``\Verb+Mon, 04 Sep 2006 15:13:13 -0700+''. 1.249 +\item[\tplkwfilt{node}{short}] Changeset hash. Yield the short form 1.250 + of a changeset hash, i.e.~a 12-byte hexadecimal string. 1.251 +\item[\tplkwfilt{date}{shortdate}] \tplkword{date} keyword. Render 1.252 + the year, month, and day of the date. Yields a string like 1.253 + ``\Verb+2006-09-04+''. 1.254 +\item[\tplfilter{strip}] Any text. Strip all leading and trailing 1.255 + whitespace from the string. 1.256 +\item[\tplfilter{tabindent}] Any text. Yield the text, with every line 1.257 + except the first starting with a tab character. 1.258 +\item[\tplfilter{urlescape}] Any text. Escape all characters that are 1.259 + considered ``special'' by URL parsers. For example, \Verb+foo bar+ 1.260 + becomes \Verb+foo%20bar+. 1.261 +\item[\tplkwfilt{author}{user}] Any text, but most useful for the 1.262 + \tplkword{author} keyword. Return the ``user'' portion of an email 1.263 + address. For example, 1.264 + ``\Verb+Bryan O'Sullivan <bos@serpentine.com>+'' becomes 1.265 + ``\Verb+bos+''. 1.266 +\end{itemize} 1.267 + 1.268 +\begin{figure} 1.269 + \interaction{template.simple.manyfilters} 1.270 + \caption{Template filters in action} 1.271 + \label{fig:template:filters} 1.272 +\end{figure} 1.273 + 1.274 +\begin{note} 1.275 + If you try to apply a filter to a piece of data that it cannot 1.276 + process, Mercurial will fail and print a Python exception. For 1.277 + example, trying to run the output of the \tplkword{desc} keyword 1.278 + into the \tplkwfilt{date}{isodate} filter is not a good idea. 1.279 +\end{note} 1.280 + 1.281 +\subsection{Combining filters} 1.282 + 1.283 +It is easy to combine filters to yield output in the form you would 1.284 +like. The following chain of filters tidies up a description, then 1.285 +makes sure that it fits cleanly into 68 columns, then indents it by a 1.286 +further 8~characters (at least on Unix-like systems, where a tab is 1.287 +conventionally 8~characters wide). 1.288 + 1.289 +\interaction{template.simple.combine} 1.290 + 1.291 +Note the use of ``\Verb+\t+'' (a tab character) in the template to 1.292 +force the first line to be indented; this is necessary since 1.293 +\tplkword{tabindent} indents all lines \emph{except} the first. 1.294 + 1.295 +Keep in mind that the order of filters in a chain is significant. The 1.296 +first filter is applied to the result of the keyword; the second to 1.297 +the result of the first filter; and so on. For example, using 1.298 +\Verb+fill68|tabindent+ gives very different results from 1.299 +\Verb+tabindent|fill68+. 1.300 + 1.301 + 1.302 +\section{From templates to styles} 1.303 + 1.304 +A command line template provides a quick and simple way to format some 1.305 +output. Templates can become verbose, though, and it's useful to be 1.306 +able to give a template a name. A style file is a template with a 1.307 +name, stored in a file. 1.308 + 1.309 +More than that, using a style file unlocks the power of Mercurial's 1.310 +templating engine in ways that are not possible using the command line 1.311 +\hgopt{log}{--template} option. 1.312 + 1.313 +\subsection{The simplest of style files} 1.314 + 1.315 +Our simple style file contains just one line: 1.316 + 1.317 +\interaction{template.simple.rev} 1.318 + 1.319 +This tells Mercurial, ``if you're printing a changeset, use the text 1.320 +on the right as the template''. 1.321 + 1.322 +\subsection{Style file syntax} 1.323 + 1.324 +The syntax rules for a style file are simple. 1.325 + 1.326 +\begin{itemize} 1.327 +\item The file is processed one line at a time. 1.328 + 1.329 +\item Leading and trailing white space are ignored. 1.330 + 1.331 +\item Empty lines are skipped. 1.332 + 1.333 +\item If a line starts with either of the characters ``\texttt{\#}'' or 1.334 + ``\texttt{;}'', the entire line is treated as a comment, and skipped 1.335 + as if empty. 1.336 + 1.337 +\item A line starts with a keyword. This must start with an 1.338 + alphabetic character or underscore, and can subsequently contain any 1.339 + alphanumeric character or underscore. (In regexp notation, a 1.340 + keyword must match \Verb+[A-Za-z_][A-Za-z0-9_]*+.) 1.341 + 1.342 +\item The next element must be an ``\texttt{=}'' character, which can 1.343 + be preceded or followed by an arbitrary amount of white space. 1.344 + 1.345 +\item If the rest of the line starts and ends with matching quote 1.346 + characters (either single or double quote), it is treated as a 1.347 + template body. 1.348 + 1.349 +\item If the rest of the line \emph{does not} start with a quote 1.350 + character, it is treated as the name of a file; the contents of this 1.351 + file will be read and used as a template body. 1.352 +\end{itemize} 1.353 + 1.354 +\section{Style files by example} 1.355 + 1.356 +To illustrate how to write a style file, we will construct a few by 1.357 +example. Rather than provide a complete style file and walk through 1.358 +it, we'll mirror the usual process of developing a style file by 1.359 +starting with something very simple, and walking through a series of 1.360 +successively more complete examples. 1.361 + 1.362 +\subsection{Identifying mistakes in style files} 1.363 + 1.364 +If Mercurial encounters a problem in a style file you are working on, 1.365 +it prints a terse error message that, once you figure out what it 1.366 +means, is actually quite useful. 1.367 + 1.368 +\interaction{template.svnstyle.syntax.input} 1.369 + 1.370 +Notice that \filename{broken.style} attempts to define a 1.371 +\texttt{changeset} keyword, but forgets to give any content for it. 1.372 +When instructed to use this style file, Mercurial promptly complains. 1.373 + 1.374 +\interaction{template.svnstyle.syntax.error} 1.375 + 1.376 +This error message looks intimidating, but it is not too hard to 1.377 +follow. 1.378 + 1.379 +\begin{itemize} 1.380 +\item The first component is simply Mercurial's way of saying ``I am 1.381 + giving up''. 1.382 + \begin{codesample4} 1.383 + \textbf{abort:} broken.style:1: parse error 1.384 + \end{codesample4} 1.385 + 1.386 +\item Next comes the name of the style file that contains the error. 1.387 + \begin{codesample4} 1.388 + abort: \textbf{broken.style}:1: parse error 1.389 + \end{codesample4} 1.390 + 1.391 +\item Following the file name is the line number where the error was 1.392 + encountered. 1.393 + \begin{codesample4} 1.394 + abort: broken.style:\textbf{1}: parse error 1.395 + \end{codesample4} 1.396 + 1.397 +\item Finally, a description of what went wrong. 1.398 + \begin{codesample4} 1.399 + abort: broken.style:1: \textbf{parse error} 1.400 + \end{codesample4} 1.401 + The description of the problem is not always clear (as in this 1.402 + case), but even when it is cryptic, it is almost always trivial to 1.403 + visually inspect the offending line in the style file and see what 1.404 + is wrong. 1.405 +\end{itemize} 1.406 + 1.407 +\subsection{Uniquely identifying a repository} 1.408 + 1.409 +If you would like to be able to identify a Mercurial repository 1.410 +``fairly uniquely'' using a short string as an identifier, you can 1.411 +use the first revision in the repository. 1.412 +\interaction{template.svnstyle.id} 1.413 +This is not guaranteed to be unique, but it is nevertheless useful in 1.414 +many cases. 1.415 +\begin{itemize} 1.416 +\item It will not work in a completely empty repository, because such 1.417 + a repository does not have a revision~zero. 1.418 +\item Neither will it work in the (extremely rare) case where a 1.419 + repository is a merge of two or more formerly independent 1.420 + repositories, and you still have those repositories around. 1.421 +\end{itemize} 1.422 +Here are some uses to which you could put this identifier: 1.423 +\begin{itemize} 1.424 +\item As a key into a table for a database that manages repositories 1.425 + on a server. 1.426 +\item As half of a \{\emph{repository~ID}, \emph{revision~ID}\} tuple. 1.427 + Save this information away when you run an automated build or other 1.428 + activity, so that you can ``replay'' the build later if necessary. 1.429 +\end{itemize} 1.430 + 1.431 +\subsection{Mimicking Subversion's output} 1.432 + 1.433 +Let's try to emulate the default output format used by another 1.434 +revision control tool, Subversion. 1.435 +\interaction{template.svnstyle.short} 1.436 + 1.437 +Since Subversion's output style is fairly simple, it is easy to 1.438 +copy-and-paste a hunk of its output into a file, and replace the text 1.439 +produced above by Subversion with the template values we'd like to see 1.440 +expanded. 1.441 +\interaction{template.svnstyle.template} 1.442 + 1.443 +There are a few small ways in which this template deviates from the 1.444 +output produced by Subversion. 1.445 +\begin{itemize} 1.446 +\item Subversion prints a ``readable'' date (the ``\texttt{Wed, 27 Sep 1.447 + 2006}'' in the example output above) in parentheses. Mercurial's 1.448 + templating engine does not provide a way to display a date in this 1.449 + format without also printing the time and time zone. 1.450 +\item We emulate Subversion's printing of ``separator'' lines full of 1.451 + ``\texttt{-}'' characters by ending the template with such a line. 1.452 + We use the templating engine's \tplkword{header} keyword to print a 1.453 + separator line as the first line of output (see below), thus 1.454 + achieving similar output to Subversion. 1.455 +\item Subversion's output includes a count in the header of the number 1.456 + of lines in the commit message. We cannot replicate this in 1.457 + Mercurial; the templating engine does not currently provide a filter 1.458 + that counts the number of items it is passed. 1.459 +\end{itemize} 1.460 +It took me no more than a minute or two of work to replace literal 1.461 +text from an example of Subversion's output with some keywords and 1.462 +filters to give the template above. The style file simply refers to 1.463 +the template. 1.464 +\interaction{template.svnstyle.style} 1.465 + 1.466 +We could have included the text of the template file directly in the 1.467 +style file by enclosing it in quotes and replacing the newlines with 1.468 +``\verb!\n!'' sequences, but it would have made the style file too 1.469 +difficult to read. Readability is a good guide when you're trying to 1.470 +decide whether some text belongs in a style file, or in a template 1.471 +file that the style file points to. If the style file will look too 1.472 +big or cluttered if you insert a literal piece of text, drop it into a 1.473 +template instead. 1.474 + 1.475 +%%% Local Variables: 1.476 +%%% mode: latex 1.477 +%%% TeX-master: "00book" 1.478 +%%% End: