<?php
// Build the compare.html from compare.csv
// compare.csv is easy edited with LibreOffice
// compare.html gets included in the website
/*
* Returns an array with fields
* Parses something like: "a,a",,"b","c" => array('a,a', '', 'b', 'c')
*/
/*
function parse_csv_line($line)
{
$ret = array();
$pos = 0;
$f = '';
$first = TRUE;
$len = strlen($line);
$last_was_comma = FALSE;
while (1) {
//echo "REST: " . substr($line, $pos) . "\n";
if (strncmp($line[$pos], '"', 1) == 0) {
$q = strpos($line, '"', $pos + 1);
//echo "Q q=" . $q . "\n";
if ($q === FALSE) {
echo 'Line: ' . $line . ' is malformed (unterminated quoted string)!' . "\n";
return FALSE;
}
$ret[] = substr($line, $pos + 1, $q - $pos - 1);
$pos = $q + 1;
if ($pos == $len)
break;
if (strncmp($line[$pos], ',', 1) != 0) {
echo 'Line: ' . $line . ' is malformed (no comma after quote)!' . "\n";
return FALSE;
}
$pos++; // skip comma
} else {
// This field does not start with '"' => starts with a comma! Search till next ','
$q = strpos($line, ',', $pos);
//echo "X q=" . $q . "\n";
if ($q === FALSE) {
// Final element after last ','
$ret[] = substr($line, $pos);
break;
}
$last_was_comma = TRUE;
$ret[] = substr($line, $pos, $q - $pos);
$pos = $q + 1;
if ($pos == $len)
break;
}
}
if ($last_was_comma)
$ret[] = '';
return $ret;
}
$s = 'a';
$r = parse_csv_line($s);
echo $s . ':'; print_r($r);
$s = ',b';
$r = parse_csv_line($s);
echo $s . ':'; print_r($r);
$s = '"a","b","c';
$r = parse_csv_line($s);
echo $s . ':'; print_r($r);
$s = '"a","b","c"';
$r = parse_csv_line($s);
echo $s . ':'; print_r($r);
$s = ',"a,a","b 3 6,6","c d e c",,';
$r = parse_csv_line($s);
echo $s . ':'; print_r($r);
$s = '"a",,';
$r = parse_csv_line($s);
echo $s . ':'; print_r($r);
exit(1);
*/
$INC = dirname(__FILE__) . '/../inc';
require_once($INC . '/util.inc.php');
function cell($a)
{
$a = rg_xss_safe($a);
// links
$a = preg_replace('/\[\[([^ ]*) ([^ ]*)\]\]/U', '<a href="\2" target="_blank">\1</a>', $a);
$a = preg_replace('/\[\[(.*)\]\]/U', '<a href="\1" target="_blank">\1</a>', $a);
// baloons
$a = preg_replace('/{(.*)}/U',
'<span class="compare_info" title="\1">?</span>', $a);
return $a;
}
$h = fopen($_SERVER['argv'][1], 'r');
if (!$h)
die('Cannot open input file!' . "\n");
$out = fopen($_SERVER['argv'][2], 'w');
if (!$out)
die('Cannot open out file!' . "\n");
fwrite($out, '<div class="main_title">Git hosting solutions comparison</div>' . "\n");
fwrite($out, '<div class="compare_intro">'
. '<i>' . "\n"
. '- This document was generated on ' . gmdate('Y-m-d') . '.<br />' . "\n"
. '- To contribute to this document, just e-mail us to'
. ' in@rocketgit.com or clone the RocketGit'
. ' <a href="https://rocketgit.com/user/catalinux/rocketgit">repository</a>'
. ', make changes and push them.<br />' . "\n"
. '- Move the mouse over the <span class="compare_info">?</span> signs'
. ' for more information.'
. '</i>' . "\n"
. '</div>' . "\n");
fwrite($out, '<table class="compare">' . "\n");
$lineno = 1;
while (($line = fgetcsv($h)) !== FALSE) {
if ($lineno == 1) {
// First line
$rows = count($line);
}
// Insert an empty line
if (empty($line[0])) {
fwrite($out, '<tr><td colspan="'
. $rows . '"> </td></tr>' . "\n");
continue;
}
// Notes
if (strcmp($line[0], '[Notes]') == 0) {
fwrite($out,
'<tr><td class="compare_chapter"'
. ' colspan="' . $rows . '">'
. cell(substr($line[0], 1, -1))
. '</td></tr>' . "\n");
continue;
}
if (strcmp($line[0], '*') == 0) {
fwrite($out,
'<tr><td colspan="' . $rows . '">'
. cell($line[1])
. '</td></tr>' . "\n");
continue;
}
if (strncmp($line[0], '[', 1) == 0) {
// We have a chapter
$line[0] = str_replace('[', '', $line[0]);
$line[0] = str_replace(']', '', $line[0]);
$class2 = ' class="compare_chapter"';
} else {
$class2 = '';
}
$block = '<tr>';
foreach ($line as $i => $f) {
if (strstr($f, '/#')) {
$t = explode('/#', $f);
$class = ' class="compare_' . $t[1] . '"';
$f = $t[0];
} else if ($i && strncasecmp($f, 'no', 2) == 0) {
$class = ' class="compare_bad"';
} else if ($i && strncasecmp($f, 'yes', 3) == 0) {
$class = ' class="compare_good"';
} else {
$class = '';
}
$block .= '<td' . $class . $class2 . '>' . cell($f) . '</td>';
}
$block .= '</tr>' . "\n";
fwrite($out, $block);
$lineno++;
}
fwrite($out, '</table>' . "\n");
fclose($out);
fclose($h);