<?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');
$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>'
. '<b>Notes</b>:<br />'
. '- 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 />'
. '- Move mouse over features field for more information.'
. '</div>' . "\n");
fwrite($out, '<table class="compare">' . "\n");
$lineno = 1;
while (($line = fgetcsv($h)) !== FALSE) {
if ($lineno == 1) {
// First line
$td = 'th';
$rows = count($line);
} else {
$td = 'td';
}
// Insert an empty line
if (empty($line[0])) {
fwrite($out, '<tr><' . $td . ' colspan="'
. $rows . '"> </' . $td . '></tr>' . "\n");
continue;
}
if (empty($line[1])) {
// We have a chapter
$title = str_replace('[', '', $line[0]);
$title = str_replace(']', '', $title);
fwrite($out,
'<tr><' . $td . ' colspan="' . $rows . '">'
. '<b>' . rg_xss_safe($title) . '</b>'
. '</' . $td . '></tr>' . "\n");
$lineno++;
continue;
}
// Notes
if (strcmp($line[0], '*') == 0) {
fwrite($out,
'<tr><' . $td . ' colspan="' . $rows . '">'
. rg_xss_safe($line[1])
. '</' . $td . '></tr>' . "\n");
continue;
}
fwrite($out, '<tr>');
foreach ($line as $i => $f) {
if ($i == 0) {
$color = '';
// Do we have a {...} - used for baloons
$p = strpos($f, '{');
if ($p) {
$q = strpos($f, '}');
$baloon = trim(substr($f, $p + 1, $q - $p - 1));
$newf = trim(substr($f, 0, $p));
$f = '<span title="' . rg_xss_safe($baloon) . '">'
. $newf . '</span>';
}
} else if (strstr($f, '/#')) {
$t = explode('/#', $f);
$color = ' bgcolor="#' . $t[1] . '"';
$f = rg_xss_safe($t[0]);
} else if (stristr($f, 'no')) {
$color = ' bgcolor="#f00"';
$f = rg_xss_safe($f);
} else if (stristr($f, 'yes')) {
$color = ' bgcolor="#0f0"';
$f = rg_xss_safe($f);
} else {
$color = '';
$f = rg_xss_safe($f);
}
fwrite($out, '<' . $td . $color . '>' . $f . '</' . $td . '>');
}
fwrite($out, '</tr>' . "\n");
$lineno++;
}
fwrite($out, '</table>' . "\n");
fclose($out);
fclose($h);
?>