[PATCH v4 5/5] git-remote-mediawiki: Add preview subcommand into git mw.

2013-06-20 Thread benoit . person
From: Benoit Person benoit.per...@ensimag.fr

In the current state, a user of git-remote-mediawiki can edit the markup text
locally, but has to push to the remote wiki to see how the page is rendererd.
Add a new 'git mw preview' command that allows rendering the markup text on
the remote wiki without actually pushing any change on the wiki.

This uses Mediawiki's API to render the markup and inserts it in an actual
HTML page from the wiki so that CSS can be rendered properly. Most links
should work when the page exists on the remote.

Signed-off-by: Benoit Person benoit.per...@ensimag.fr
Signed-off-by: Matthieu Moy matthieu@grenoble-inp.fr

---
 contrib/mw-to-git/Git/Mediawiki.pm |   3 +-
 contrib/mw-to-git/git-mw.perl  | 301 -
 2 files changed, 302 insertions(+), 2 deletions(-)

diff --git a/contrib/mw-to-git/Git/Mediawiki.pm 
b/contrib/mw-to-git/Git/Mediawiki.pm
index 47fe4f4..d13c4df 100644
--- a/contrib/mw-to-git/Git/Mediawiki.pm
+++ b/contrib/mw-to-git/Git/Mediawiki.pm
@@ -19,7 +19,7 @@ require Exporter;
 
 # Methods which can be called as standalone functions as well:
 @EXPORT_OK = qw(clean_filename smudge_filename connect_maybe
-   EMPTY HTTP_CODE_OK);
+   EMPTY HTTP_CODE_OK HTTP_CODE_PAGE_NOT_FOUND);
 }
 
 # Mediawiki filenames can contain forward slashes. This variable decides by 
which pattern they should be replaced
@@ -30,6 +30,7 @@ use constant EMPTY = q{};
 
 # HTTP codes
 use constant HTTP_CODE_OK = 200;
+use constant HTTP_CODE_PAGE_NOT_FOUND = 404;
 
 sub clean_filename {
my $filename = shift;
diff --git a/contrib/mw-to-git/git-mw.perl b/contrib/mw-to-git/git-mw.perl
index 4a3e4a9..d4f412a 100644
--- a/contrib/mw-to-git/git-mw.perl
+++ b/contrib/mw-to-git/git-mw.perl
@@ -12,6 +12,14 @@ use strict;
 use warnings;
 
 use Getopt::Long;
+use URI::URL qw(url);
+use LWP::UserAgent;
+use HTML::TreeBuilder;
+
+use Git;
+use MediaWiki::API;
+use Git::Mediawiki qw(clean_filename connect_maybe
+   EMPTY HTTP_CODE_PAGE_NOT_FOUND);
 
 # By default, use UTF-8 to communicate with Git and the user
 binmode STDERR, ':encoding(UTF-8)';
@@ -26,9 +34,26 @@ sub v_print {
return;
 }
 
+# Preview parameters
+my $file_name = EMPTY;
+my $remote_name = EMPTY;
+my $preview_file_name = EMPTY;
+my $autoload = 0;
+sub file {
+   $file_name = shift;
+   return $file_name;
+}
+
 my %commands = (
'help' =
-   [\help, {}, \help]
+   [\help, {}, \help],
+   'preview' =
+   [\preview, {
+   '' = \file,
+   'output|o=s' = \$preview_file_name,
+   'remote|r=s' = \$remote_name,
+   'autoload|a' = \$autoload
+   }, \preview_help]
 );
 
 # Search for sub-command
@@ -47,6 +72,279 @@ GetOptions( %{$cmd-[1]},
 # Launch command
 {$cmd-[0]};
 
+# Preview Functions 

+
+# @TODO : add documentation for verbose option
+sub preview_help {
+   print {*STDOUT} 'END';
+USAGE: git mw preview [--remote|-r remote name] [--autoload|-a]
+  [--output|-o output filename] [--verbose|-v]
+  blob | filename
+
+DESCRIPTION:
+Preview is an utiliy to preview local content of a mediawiki repo as if it was
+pushed on the remote.
+
+For that, preview searches for the remote name of the current branch's upstream
+if --remote is not set. If that remote is not found or if it is not a 
mediawiki,
+it lists all mediawiki remotes configured and asks you to replay your command
+with the --remote option set properly.
+
+Then, it searches for a file named 'filename'. If it's not found in the current
+dir, it will assume it's a blob.
+
+The content retrieved in the file (or in the blob) will then be parsed by the
+distant mediawiki and combined with a template retrieved from the mediawiki.
+
+Finally, preview will save the HTML result in a file. and autoload it in your
+default web browser if the option --autoload is present.
+
+OPTIONS:
+   -r remote name, --remote remote name
+   If the remote is a mediawiki, the template and the parse engine 
used for
+   the preview will be those of that remote.
+   If not, a list of valid remotes will be shown.
+
+   -a, --autoload
+   Try to load the HTML output in a new tab (or new window) of 
your default
+   web browser.
+
+   -o output filename, --output output filename
+   Change the HTML output filename. Default filename is based on 
the input
+   filename with its extension replaced by '.html'.
+
+   -v, --verbose
+   Show more information on what's going on under the hood.
+END
+   exit;
+}
+
+sub preview {
+   my $wiki;
+   my ($remote_url, $wiki_page_name);
+   my ($new_content, $template);
+ 

Re: [PATCH v4 5/5] git-remote-mediawiki: Add preview subcommand into git mw.

2013-06-20 Thread Matthieu Moy
benoit.per...@ensimag.fr writes:

 +# @TODO : add documentation for verbose option

I guess that's not applicable anymore.

 +distant mediawiki and combined with a template retrieved from the mediawiki.

s/distant/remote/

 +sub preview {
 + my $wiki;
 + my ($remote_url, $wiki_page_name);
 + my ($new_content, $template);
 + my $file_content;
 +
 + if ($file_name eq EMPTY) {
 + die Missing file argument, see `git mw help` \n;

Unneeded space before \n.

 + for (@{ $html_tree-extract_links() }) {
 + my ($link, $element, $attr) = @{ $_ };
 + my $url = url($link)-canonical;
 + $element-attr($attr, URI-new_abs($url, $remote_url));
 + }

Extracting this into a function like make_links_absolute would have
made it clearer.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 5/5] git-remote-mediawiki: Add preview subcommand into git mw.

2013-06-20 Thread Matthieu Moy
benoit.per...@ensimag.fr writes:

 + for (@{ $html_tree-extract_links() }) {
 + my ($link, $element, $attr) = @{ $_ };
 + my $url = url($link)-canonical;
 + $element-attr($attr, URI-new_abs($url, $remote_url));

Actually, this breaks some relative links, like table of content links,
because you consider them relative to URLs like http://example.com/wiki/
but not http://example.com/wiki/index.php/My_Page.

Ideally, links pointing to stg like #anchor should not be modified.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html