diff options
author | Marcin Owsiany <porridge> | 2011-07-30 13:13:25 +0000 |
---|---|---|
committer | Marcin Owsiany <porridge> | 2011-07-30 13:13:25 +0000 |
commit | c998594bfcfde60f3fbac9b98b9a72bcf3be60e1 (patch) | |
tree | 352ad4e5bb55f0a85443cc1ead8ed1fe91f4c368 | |
parent | 2605c5238f91205edeae6bf9374d54459309f0bc (diff) |
english/devel/website/stats/anoncvs-cors
CVS version numbers
stattrans.pl: 1.104 -> 1.105
english/devel/website/stats/Makefile: 1.4 -> 1.5
english/devel/website/stats/diffstat.js: INITIAL -> 1.1
-rw-r--r-- | english/devel/website/stats/Makefile | 9 | ||||
-rw-r--r-- | english/devel/website/stats/diffstat.js | 125 | ||||
-rwxr-xr-x | stattrans.pl | 5 |
3 files changed, 138 insertions, 1 deletions
diff --git a/english/devel/website/stats/Makefile b/english/devel/website/stats/Makefile index 4e103546f26..03c7ea06111 100644 --- a/english/devel/website/stats/Makefile +++ b/english/devel/website/stats/Makefile @@ -35,3 +35,12 @@ endif all:: $(STATS) install:: $(DESTSTATS) + +ifeq "$(LANGUAGE)" "en" +install:: $(HTMLDIR)/diffstat.js +$(HTMLDIR)/diffstat.js: diffstat.js + @test -d $(HTMLDIR) || mkdir -m g+w -p $(HTMLDIR) + @echo copying $(@F) to $(HTMLDIR) + -@install -m 664 -p $(@F) $(HTMLDIR) +endif + diff --git a/english/devel/website/stats/diffstat.js b/english/devel/website/stats/diffstat.js new file mode 100644 index 00000000000..ec458f47340 --- /dev/null +++ b/english/devel/website/stats/diffstat.js @@ -0,0 +1,125 @@ +// GETs the given url, and schedules the callback to be invoked with the +// response body when it arrives. +// +// In case of problems the callback is invoked with an Error() argument, +// containing the error message. +// TODO: This is ugly, but how else to asynchronously deliver an exceptional +// condition? +// +// Args: +// url: the url to GET +// callback: the function to be invoked with the body or Error as an argument +function getTextAsync(url, callback) { + var request = new XMLHttpRequest(); + request.onreadystatechange = function() { + if (request.readyState !== 4) + return; + if (request.status === 200) { + var type = request.getResponseHeader("Content-Type"); + if (type.match(/^text/)) + callback(request.responseText); + else + // TODO: i18nize + callback(Error('Non-text response received')); + } else { + if (request.statusText == "") + // TODO: i18nize + callback(Error("Unknown error")); + else + callback(Error(request.statusText)); + } + }; + request.open("GET", url); + request.send(null); +} + +// Calculate the number of additions and removals in a diff text. +// Args: +// diff: the text of the diff, optionally including the diff header (i.e. the +// ---/+++ or other lines) +// Returns: +// an array of two elements: number of added lines, number of removed lines. +// On error a null is returned instead of the array +function diffstat(diff) { + if (diff == null) + return null; + var first_hunk_offset = diff.indexOf("\n@@"); + var diff_text; + if (first_hunk_offset > 0) { + // Skip the ---/+++ in diff header. + diff_text = diff.substring(first_hunk_offset); + } else { + diff_text = diff; + } + var changes = diff_text.match(/\n[+-]/g); + if (changes == null) { + return null; + } + function count_adds(i, str) { return str == "\n+" ? i + 1 : i } + function count_dels(i, str) { return str == "\n-" ? i + 1 : i } + var additions = changes.reduce(count_adds, 0); + var removals = changes.reduce(count_dels, 0); + return [additions, removals]; +} + +// Returns an HTML element presenting the given diff text or error object. +// +// Args: +// diff_or_error: the diff text to pass to diffstat() or an error message +// Returns: +// an HTML element, containing information about the argument +function diffstat_pretty(diff_or_error) { + var topspan = document.createElement('span'); + topspan.setAttribute('style', 'font-family: monospace'); + if (diff_or_error instanceof Error) { + var ret; + ret = document.createElement('span'); + ret.setAttribute('title', diff_or_error.message); + ret.innerText = ':-('; + topspan.appendChild(ret); + } else { + var counts = diffstat(diff_or_error); + if (counts == null) { + var ret; + ret = document.createElement('span'); + ret.setAttribute('title', 'parse error'); + ret.innerText = ':-('; + topspan.appendChild(ret); + } else { + var ret; + + ret = document.createElement('span'); + ret.setAttribute('style', 'color: green'); + ret.innerText = '+' + counts[0]; + topspan.appendChild(ret); + + // ret = document.createElement('br'); + ret = document.createTextNode('/'); + topspan.appendChild(ret); + + ret = document.createElement('span'); + ret.setAttribute('style', 'color: red'); + ret.innerText = '-' + counts[1]; + topspan.appendChild(ret); + } + } + return topspan; +} + +// Replaces contents of element with diffstat information of the file between the revisions. +// +// Args: +// path: path to the original file, relative to the english directory +// r1, r2: the revision strings to compare +// element: the element which is populated with the diffstat information +function setDiffstat(path, r1, r2, element) { + // does not send CORS: + // var base = "http://anonscm.debian.org/viewvc/webwml/webwml/"; + // does not allow ExecCGI: + // var base = "http://people.debian.org/~porridge/cgi-bin/webwml.cgi/"; + var base = "http://webwml.alioth.debian.org/cgi-bin/anoncvs-cors/"; + var lang = "english"; + var url = base + lang + "/" + path + "?r1=" + r1 + "&r2=" + r2 + "&view=patch"; + element.innerHTML = '...'; + getTextAsync(url, function(text) { element.replaceChild(diffstat_pretty(text), element.firstChild); }); +} diff --git a/stattrans.pl b/stattrans.pl index b8e5d4cc9d9..cb673071f5f 100755 --- a/stattrans.pl +++ b/stattrans.pl @@ -386,6 +386,7 @@ foreach $lang (@search_in) { $o_body .= sprintf "<td>%s</td>", $transversion{"$lang/$file"}; $o_body .= sprintf "<td>%s</td>", $version{"$orig/$file"}; $o_body .= sprintf "<td>%s</td>", $msg; + $o_body .= sprintf "<td style='font-family: monospace' title='<gettext domain=\"stats\">Click to fetch diffstat data.</gettext>' onClick=\"setDiffstat('%s', '%s', '%s', this)\">+/-</td>", $file, $transversion{"$lang/$file"}, $version{"$orig/$file"}; if ($msg eq '<gettext domain="stats">Wrong translation version</gettext>' || $msg eq '<gettext domain="stats">The original no longer exists</gettext>') { $o_body .= "<td></td><td></td>"; } else { @@ -475,7 +476,8 @@ foreach $lang (@search_in) { if (open (HTML, ">$config{'htmldir'}/$l.wml")) { printf HTML "#use wml::debian::template title=\"<:=\$trans{\$CUR_ISO_LANG}{%s}:>\"\n", $lang; - print HTML "#use wml::debian::toc\n\n"; + print HTML "#use wml::debian::toc\n"; + print HTML "<script src='diffstat.js'></script>\n\n"; $color = get_color ($percent_a{$lang}); printf HTML '<table summary="<gettext domain="stats">Translation summary for</gettext> <:=$trans{$CUR_ISO_LANG}{'.$lang.'} :>" style="background-color: %s; width: 100%; font-weight: bold; margin: 0; text-align: center;">'."\n", $color; @@ -509,6 +511,7 @@ foreach $lang (@search_in) { print HTML '<th><gettext domain="stats">Translation</gettext></th>'."\n"; print HTML '<th><gettext domain="stats">Origin</gettext></th>'."\n"; print HTML '<th><gettext domain="stats">Comment</gettext></th>'."\n"; + print HTML '<th><gettext domain="stats">Diffstat</gettext></th>'."\n"; if ($opt_d eq "u") { print HTML '<th><gettext domain="stats">Unified diff</gettext></th><th><gettext domain="stats">Colored diff</gettext></th>'; } elsif ($opt_d eq "h") { print HTML '<th><gettext domain="stats">Colored diff</gettext></th><th><gettext domain="stats">Unified diff</gettext></th>'; } else { print HTML '<th><gettext domain="stats">Diff</gettext></th>'; } |