#!/usr/bin/env python

import os
import sys
from optparse import HelpFormatter

sys.path.append(os.path.abspath('static/'))

import upload


class GCWikiHelpFormatter (HelpFormatter):
    """Format help with wiki markup for Google Code."""

    def __init__(self,
                 indent_increment=2,
                 max_help_position=24,
                 width=None,
                 short_first=1):
        HelpFormatter.__init__(
            self, indent_increment, max_help_position, width, short_first)
        self._dl_open = False

    def indent(self):
        self._pending = 'INDENT'
        HelpFormatter.indent(self)

    def dedent(self):
        self._pending = 'DEDENT'
        HelpFormatter.dedent(self)

    def format_usage(self, usage):
        return "*Usage summary:* `%s`\n" % usage

    def format_heading(self, heading):
        if self._dl_open:
            pre = '\n</dl>\n'
        else:
            pre = ''
        markup = '='*(self.current_indent+2)
        self._dl_open = True
        return "%s%s %s %s\n<dl>\n" % (pre, markup, heading, markup)

    def format_option(self, option):
        result = []
        opts = self.option_strings[option]
        result.append('<dt>`%s`</dt>\n' % opts)
        if option.help:
            help_text = '<dd>%s</dd>\n' % self.expand_default(option)
            result.append(help_text)
        return ''.join(result)


def main():
    upload.parser.formatter = GCWikiHelpFormatter()
    print HEADER
    print upload.parser.format_option_help()
    print '</dl>'  # TODO: Formatter should do this
    print

HEADER = """#summary upload.py usage and options.
<wiki:comment>
THIS PAGE IS AUTOGENERATED. DO NOT EDIT.
To update this page run tools/uploadopts2wiki.py
</wiki:comment>

= upload.py Usage =

[http://codereview.appspot.com/static/upload.py upload.py] is a tool
for uploading diffs from a version control system to the codereview app.

*Usage summary:*

{{{upload.py [options] [-- diff_options]}}}

Diff options are passed to the diff command of the underlying system.

*Supported version control systems:*
 * Git
 * Mercurial
 * Subversion

It is important for Git/Mercurial users to specify a tree/node/branch to diff
against by using the '--rev' option.

"""


if __name__ == '__main__':
    main()

@@ -77,50 +77,98 @@
 er%0A%0A
-sys.path.append(os.path.abspath('static/')
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))%0Asys.path.insert(0, ROOT
 )%0A%0Ai
@@ -1612,16 +1612,33 @@
 do this%0A
+    print FOOTER%0A
     prin
@@ -1639,16 +1639,16 @@
   print%0A
-
 %0AHEADER 
@@ -2180,16 +2180,28 @@
 bversion
+%0A * Perforce
 %0A%0AIt is 
@@ -2315,16 +2315,684 @@
 .%0A%0A%22%22%22%0A%0A
+FOOTER = %22%22%22%5C%0A%0A==== Running upload.py from perforce GUIs ====%0A%0AYou can right click on a perforce changelist and create a new Rietveld code review by adding a custom tool with the following settings:%0A%0AApplication: python Arguments: /PATH/TO/upload.py -s MY_SERVER --p4_changelist %25p --p4_port $p --p4_user $u --p4_client $c Start In: empty. Check %22Add to applicable context menus%22, %22Run tool in terminal window%22 (or system equivalent), and %22Ignore P4CONFIG files%22.%0A%0AReplace /PATH/TO/ with the location of upload.py, and MY_SERVER with the rietveld code review server. See screenshot %5Bhttp://alexmccarthy.net/Rietveld%2520-%2520P4V%2520Custom%2520Tool%2520Settings.png here%5D.%0A%22%22%22%0A
 %0Aif __na

Update tools/uploadopts2wiki.py