From patchwork Sat Feb 9 13:00:40 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [6,of,6] highlight: use docutils for generating help if possible From: Dan Villiom Podlaski Christiansen X-Patchwork-Id: 864 Message-Id: <40da011562a84f69cd3c.1360414840@s0-0.paconsult7.bbnplanet.net> To: Mercurial Devel Date: Sat, 09 Feb 2013 13:00:40 +0000 # HG changeset patch # User Dan Villiom Podlaski Christiansen # Date 1360414108 0 # Node ID 40da011562a84f69cd3c198e9c44cd4545bdeedb # Parent 45b34ea40358b6f8fe14ef874ebbc5de5556f23c highlight: use docutils for generating help if possible diff --git a/hgext/highlight/__init__.py b/hgext/highlight/__init__.py --- a/hgext/highlight/__init__.py +++ b/hgext/highlight/__init__.py @@ -21,9 +21,13 @@ There is a single configuration option:: The default is 'colorful'. """ -import highlight +import highlight, rst + from mercurial.hgweb import webcommands, webutil, common -from mercurial import extensions, encoding +from mercurial import extensions, encoding, minirst + +import cgi, traceback + testedwith = 'internal' def filerevision_highlight(orig, web, tmpl, fctx): @@ -48,6 +52,24 @@ def annotate_highlight(orig, web, req, t highlight.pygmentize('annotateline', fctx, style, tmpl) return orig(web, req, tmpl) +def format_docutils(orig, *args, **opts): + if opts.get('style') == 'html': + try: + return rst.publish(args[0]) + except Exception: + # silently fallback to minirst + tb = traceback.format_exc() + + return '\n'.join(['', + '', + orig(*args, **opts)]) + else: + return orig(*args, **opts) + def generate_css(web, req, tmpl): pg_style = web.config('web', 'pygments_style', 'colorful') fmter = highlight.HtmlFormatter(style = pg_style) @@ -60,5 +82,6 @@ def extsetup(): extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) + extensions.wrapfunction(minirst, 'format', format_docutils) webcommands.highlightcss = generate_css webcommands.__all__.append('highlightcss') diff --git a/hgext/highlight/rst.py b/hgext/highlight/rst.py new file mode 100644 --- /dev/null +++ b/hgext/highlight/rst.py @@ -0,0 +1,32 @@ +# rst.py - generate HTML from docutils source +# +# Copyright 2010-2013 Matt Mackall and others +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2 or any later version. +# +# The original module was split in an interface and an implementation +# file to defer pygments loading and speedup extension setup. + +from docutils.parsers.rst import roles +from docutils import core, nodes, utils + +def role_hg(name, rawtext, text, lineno, inliner, + options={}, content=[]): + text = "hg " + utils.unescape(text) + linktext = nodes.literal(rawtext, text) + parts = text.split() + cmd, args = parts[1], parts[2:] + if cmd == 'help' and args: + cmd = args[0] # link to 'dates' for 'hg help dates' + node = nodes.reference(rawtext, '', linktext, + refuri="%s" % cmd) + return [node], [] + +roles.register_local_role("hg", role_hg) + +def publish(text, **opts): + parts = core.publish_parts(text, writer_name='html', + enable_exit_status=True, **opts) + + return parts['body'] diff --git a/tests/test-hgweb-help.t b/tests/test-highlight-help.t copy from tests/test-hgweb-help.t copy to tests/test-highlight-help.t --- a/tests/test-hgweb-help.t +++ b/tests/test-highlight-help.t @@ -1,11 +1,11 @@ - $ "$TESTDIR/hghave" serve || exit 80 - -Test the help pages in hgweb. - -Dish up an empty repo; serve it cold. - + $ "$TESTDIR/hghave" docutils serve || exit 80 + $ cat <> $HGRCPATH + > [extensions] + > highlight = + > EOF $ hg init test $ cd test + $ hg serve -n test -p $HGPORT -d --pid-file=hg.pid $ cat hg.pid >> $DAEMON_PIDS @@ -53,102 +53,114 @@ Dish up an empty repo; serve it cold. files, or words in the commit message
-

- hg add [OPTION]... [FILE]... -

-

- add the specified files on the next commit -

-

- Schedule files to be version controlled and added to the - repository. -

-

- The files will be added to the repository at the next commit. To - undo an add before that, see "hg forget". -

-

- If no names are given, add all files to the repository. -

-

- Returns 0 if all files are successfully added. -

-

- options: -

- - +

hg add [OPTION]... [FILE]...

+

add the specified files on the next commit

+
+

Schedule files to be version controlled and added to the + repository.

+

The files will be added to the repository at the next commit. To + undo an add before that, see hg forget.

+

If no names are given, add all files to the repository.

+
+

An example showing how new (unknown) files are added + automatically by hg add:

+
+  \x24 ls (esc)
+  foo.c
+  \x24 hg status (esc)
+  ? foo.c
+  \x24 hg add (esc)
+  adding foo.c
+  \x24 hg status (esc)
+  A foo.c
+  
+
+

Returns 0 if all files are successfully added.

+
+

options:

+
+
-I
+ + + + + + + - - + + + - - + + + - - + + + - + + +
-I --include PATTERN [+]include names matching the given patterns
-Xinclude names matching the given patterns
-X --exclude PATTERN [+]exclude names matching the given patterns
-Sexclude names matching the given patterns
-S --subreposrecurse into subrepositories
-nrecurse into subrepositories
-n --dry-rundo not perform actions, just print output
do not perform actions, just print output
-

- [+] marked option can be specified multiple times -

-

- global options: -

- - - - - - - - + +

[+] marked option can be specified multiple times

+

global options:

+
+
-R--repository REPOrepository root directory or name of overlay bundle file
--cwd DIRchange working directory
-y
+ + + + + + + + + + + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + +
-R--repository REPO + --cwd DIRrepository root directory or name of overlay bundle file + change working directory
-y --noninteractivedo not prompt, automatically pick the first choice for all prompts
-qdo not prompt, automatically pick the first choice for all prompts
-q --quietsuppress output
-v--verboseenable additional output
--config CONFIG [+]set/override config option (use 'section.name=value')
--debugenable debugging output
--debuggerstart debugger
--encoding ENCODEset the charset encoding (default: ascii)
--encodingmode MODEset the charset encoding mode (default: strict)
--tracebackalways print a traceback on exception
--timetime how long the command takes
--profileprint command execution profile
--versionoutput version information and exit
-h--helpdisplay help and exit
--hiddenconsider hidden changesets
suppress output
-v--verbose + --config CONFIG [+] + --debug + --debugger + --encoding ENCODE + --encodingmode MODE + --traceback + --time + --profile + --versionenable additional output + set/override config option (use 'section.name=value') + enable debugging output + start debugger + set the charset encoding (default: ascii) + set the charset encoding mode (default: strict) + always print a traceback on exception + time how long the command takes + print command execution profile + output version information and exit
-h--help + --hiddendisplay help and exit + consider hidden changesets
-

- [+] marked option can be specified multiple times -

+ +

[+] marked option can be specified multiple times

@@ -205,102 +217,156 @@ Dish up an empty repo; serve it cold. files, or words in the commit message
-

- hg remove [OPTION]... FILE... -

-

- aliases: rm -

-

- remove the specified files on the next commit -

-

- Schedule the indicated files for removal from the current branch. -

-

- This command schedules the files to be removed at the next commit. - To undo a remove before that, see "hg revert". To undo added - files, see "hg forget". -

-

- Returns 0 on success, 1 if any warnings encountered. -

-

- options: -

- - +

hg remove [OPTION]... FILE...

+

aliases: rm

+

remove the specified files on the next commit

+
+

Schedule the indicated files for removal from the current branch.

+

This command schedules the files to be removed at the next commit. + To undo a remove before that, see hg revert. To undo added + files, see hg forget.

+
+

-A/--after can be used to remove only files that have already + been deleted, -f/--force can be used to force deletion, and -Af + can be used to remove files from the next revision without + deleting them from the working directory.

+

The following table details the behavior of remove for different + file states (columns) and option combinations (rows). The file + states are Added [A], Clean [C], Modified [M] and Missing [!] + (as reported by hg status). The actions are Warn, Remove + (from branch) and Delete (from disk):

+
-A
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
     
noneWRDWR
-fRRDRDR
-AWWWR
-AfRRRR
+

Note that remove never deletes files in Added [A] state from the + working directory, not even if option --force is specified.

+
+

Returns 0 on success, 1 if any warnings encountered.

+ +

options:

+
+ + + + + + + + - - + + + - - + + + - - + + + - + + +
-A --afterrecord delete for missing files
-frecord delete for missing files
-f --forceremove (and delete) file even if added or modified
-Iremove (and delete) file even if added or modified
-I --include PATTERN [+]include names matching the given patterns
-Xinclude names matching the given patterns
-X --exclude PATTERN [+]exclude names matching the given patterns
exclude names matching the given patterns
-

- [+] marked option can be specified multiple times -

-

- global options: -

- - - - - - - - + +

[+] marked option can be specified multiple times

+

global options:

+
+
-R--repository REPOrepository root directory or name of overlay bundle file
--cwd DIRchange working directory
-y
+ + + + + + + + + + + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + +
-R--repository REPO + --cwd DIRrepository root directory or name of overlay bundle file + change working directory
-y --noninteractivedo not prompt, automatically pick the first choice for all prompts
-qdo not prompt, automatically pick the first choice for all prompts
-q --quietsuppress output
-v--verboseenable additional output
--config CONFIG [+]set/override config option (use 'section.name=value')
--debugenable debugging output
--debuggerstart debugger
--encoding ENCODEset the charset encoding (default: ascii)
--encodingmode MODEset the charset encoding mode (default: strict)
--tracebackalways print a traceback on exception
--timetime how long the command takes
--profileprint command execution profile
--versionoutput version information and exit
-h--helpdisplay help and exit
--hiddenconsider hidden changesets
suppress output
-v--verbose + --config CONFIG [+] + --debug + --debugger + --encoding ENCODE + --encodingmode MODE + --traceback + --time + --profile + --versionenable additional output + set/override config option (use 'section.name=value') + enable debugging output + start debugger + set the charset encoding (default: ascii) + set the charset encoding mode (default: strict) + always print a traceback on exception + time how long the command takes + print command execution profile + output version information and exit
-h--help + --hiddendisplay help and exit + consider hidden changesets
-

- [+] marked option can be specified multiple times -

+
+

[+] marked option can be specified multiple times

@@ -357,47 +423,28 @@ Dish up an empty repo; serve it cold. files, or words in the commit message
- - -
Specifying Single Revisions
-

- Mercurial supports several ways to specify individual revisions. -

-

- A plain integer is treated as a revision number. Negative integers are +

Mercurial supports several ways to specify individual revisions.

+

A plain integer is treated as a revision number. Negative integers are treated as sequential offsets from the tip, with -1 denoting the tip, - -2 denoting the revision prior to the tip, and so forth. -

-

- A 40-digit hexadecimal string is treated as a unique revision - identifier. -

-

- A hexadecimal string less than 40 characters long is treated as a + -2 denoting the revision prior to the tip, and so forth.

+

A 40-digit hexadecimal string is treated as a unique revision + identifier.

+

A hexadecimal string less than 40 characters long is treated as a unique revision identifier and is referred to as a short-form identifier. A short-form identifier is only valid if it is the prefix - of exactly one full-length identifier. -

-

- Any other string is treated as a bookmark, tag, or branch name. A + of exactly one full-length identifier.

+

Any other string is treated as a bookmark, tag, or branch name. A bookmark is a movable pointer to a revision. A tag is a permanent name associated with a revision. A branch name denotes the tipmost revision - of that branch. Bookmark, tag, and branch names must not contain the ":" - character. -

-

- The reserved name "tip" always identifies the most recent revision. -

-

- The reserved name "null" indicates the null revision. This is the - revision of an empty repository, and the parent of revision 0. -

-

- The reserved name "." indicates the working directory parent. If no + of that branch. Bookmark, tag, and branch names must not contain the ":" + character.

+

The reserved name "tip" always identifies the most recent revision.

+

The reserved name "null" indicates the null revision. This is the + revision of an empty repository, and the parent of revision 0.

+

The reserved name "." indicates the working directory parent. If no working directory is checked out, it is equivalent to null. If an - uncommitted merge is in progress, "." is the revision of the first - parent. -

+ uncommitted merge is in progress, "." is the revision of the first + parent.