From: Chuck Hagenbuch Date: Sat, 13 Nov 2010 18:57:57 +0000 (-0500) Subject: Encapsulate HTML diff generation in a view helper X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=c2f6c0fc56e1a6b9f4ac53981a4d025614b9b549;p=horde.git Encapsulate HTML diff generation in a view helper --- diff --git a/chora/app/helpers/Diff.php b/chora/app/helpers/Diff.php new file mode 100644 index 000000000..87b1e11a0 --- /dev/null +++ b/chora/app/helpers/Diff.php @@ -0,0 +1,86 @@ + + * @package Chora + */ +class Chora_Diff_Helper extends Horde_View_Helper_Base +{ + /** + * @var string + */ + protected $_context = ''; + + public function diff($diff) + { + if (!$diff) { + return '

' . _("No Visible Changes") . '

'; + } + + return $this->render('app/views/diff/diff.html.php', array('diff' => $diff)); + } + + public function diffAdd($change) + { + return $this->render('app/views/diff/added.html.php', array( + 'lines' => $change['lines'], + )); + } + + public function diffRemove($change) + { + return $this->render('app/views/diff/removed.html.php', array( + 'lines' => $change['lines'], + )); + } + + public function diffEmpty($change) + { + $this->_context .= $this->escape($change['line']) . '
'; + return ''; + } + + public function diffChange($change) + { + // Pop the old/new stacks one by one, until both are empty. + $oldsize = count($change['old']); + $newsize = count($change['new']); + $left = $right = ''; + for ($row = 0, $rowMax = max($oldsize, $newsize); $row < $rowMax; ++$row) { + $left .= (isset($change['old'][$row]) ? $this->escape($change['old'][$row]) : '') . '
'; + $right .= (isset($change['new'][$row]) ? $this->escape($change['new'][$row]) : '') . '
'; + } + + return $this->render('app/views/diff/change.html.php', array( + 'left' => $left, + 'right' => $right, + 'oldsize' => $oldsize, + 'newsize' => $newsize, + 'row' => $row, + )); + } + + public function hasContext() + { + return !empty($this->_context); + } + + public function diffContext() + { + $context = $this->_context; + $this->_context = ''; + + return $this->render('app/views/diff/context.html.php', array( + 'context' => $context, + )); + } + + public function diffCaption() + { + return $this->render('app/views/diff/caption.html.php'); + } +} diff --git a/chora/app/views/diff/added.html.php b/chora/app/views/diff/added.html.php new file mode 100644 index 000000000..d88932298 --- /dev/null +++ b/chora/app/views/diff/added.html.php @@ -0,0 +1,4 @@ + + +
escape($l) . '
' ?>
+ diff --git a/chora/app/views/diff/caption.html.php b/chora/app/views/diff/caption.html.php new file mode 100644 index 000000000..706eb44e2 --- /dev/null +++ b/chora/app/views/diff/caption.html.php @@ -0,0 +1,7 @@ +
+ +<?php echo _(" /> +<?php echo _(" /> +<?php echo _(" /> +<?php echo _(" /> +
diff --git a/chora/app/views/diff/change.html.php b/chora/app/views/diff/change.html.php new file mode 100644 index 000000000..d54ba5d7c --- /dev/null +++ b/chora/app/views/diff/change.html.php @@ -0,0 +1,20 @@ + + + +
+ + + + + + + + +
+ + + + + + + diff --git a/chora/app/views/diff/context.html.php b/chora/app/views/diff/context.html.php new file mode 100644 index 000000000..465893293 --- /dev/null +++ b/chora/app/views/diff/context.html.php @@ -0,0 +1,4 @@ + +
+
+ diff --git a/chora/app/views/diff/diff.html.php b/chora/app/views/diff/diff.html.php new file mode 100644 index 000000000..64c042528 --- /dev/null +++ b/chora/app/views/diff/diff.html.php @@ -0,0 +1,24 @@ + + + + + + + +hasContext() && $change['type'] != 'empty') { + echo $this->diffContext(); + } + + $method = 'diff' . ucfirst($change['type']); + echo $this->$method($change); +} + +if ($this->hasContext()) { + echo $this->diffContext(); +} +?> + + +
diff --git a/chora/app/views/diff/removed.html.php b/chora/app/views/diff/removed.html.php new file mode 100644 index 000000000..ca3d2a6b8 --- /dev/null +++ b/chora/app/views/diff/removed.html.php @@ -0,0 +1,4 @@ + +
escape($l) . '
' ?>
+ + diff --git a/chora/diff.php b/chora/diff.php index c484cfb30..7d890b2f6 100644 --- a/chora/diff.php +++ b/chora/diff.php @@ -78,14 +78,11 @@ foreach ($VC->getRevisionRange($fl, $r1, $r2) as $val) { } } -/* Get list of diff types. */ -$diff_types = array_flip($VC->availableDiffTypes()); - Horde::addScriptFile('stripe.js', 'horde'); require CHORA_TEMPLATES . '/common-header.inc'; require CHORA_TEMPLATES . '/menu.inc'; require CHORA_TEMPLATES . '/headerbar.inc'; -require CHORA_TEMPLATES . '/diff/hr/header.inc'; +require CHORA_TEMPLATES . '/diff/header.inc'; $mime_type = Horde_Mime_Magic::filenameToMIME($fullname); if (substr($mime_type, 0, 6) == 'image/') { @@ -96,74 +93,10 @@ if (substr($mime_type, 0, 6) == 'image/') { echo "\""' . "\""'; } else { - /* Retrieve the tree of changes. */ - $lns = $VC->diff($fl, $r1, $r2, array('human' => true, 'num' => $num, 'ws' => $ws)); - if (!$lns) { - /* Is the diff empty? */ - require CHORA_TEMPLATES . '/diff/hr/nochange.inc'; - } else { - /* Iterate through every header block of changes. */ - foreach ($lns as $header) { - $lefthead = $header['oldline']; - $righthead = $header['newline']; - require CHORA_TEMPLATES . '/diff/hr/row.inc'; - - /* Each header block consists of a number of changes - * (add, remove, change). */ - $curContext = ''; - foreach ($header['contents'] as $change) { - if (!empty($curContext) && $change['type'] != 'empty') { - $line = $curContext; - $curContext = ''; - require CHORA_TEMPLATES . '/diff/hr/empty.inc'; - } - - switch ($change['type']) { - case 'add': - $line = ''; - foreach ($change['lines'] as $l) { - $line .= htmlspecialchars($l) . '
'; - } - require CHORA_TEMPLATES . '/diff/hr/add.inc'; - break; - - case 'remove': - $line = ''; - foreach ($change['lines'] as $l) { - $line .= htmlspecialchars($l) . '
'; - } - require CHORA_TEMPLATES . '/diff/hr/remove.inc'; - break; - - case 'empty': - $curContext .= htmlspecialchars($change['line']) . '
'; - break; - - case 'change': - /* Pop the old/new stacks one by one, until both are - * empty. */ - $oldsize = count($change['old']); - $newsize = count($change['new']); - $left = $right = ''; - for ($row = 0, $rowMax = max($oldsize, $newsize); $row < $rowMax; ++$row) { - $left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : ''; - $left .= '
'; - $right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : ''; - $right .= '
'; - } - require CHORA_TEMPLATES . '/diff/hr/change.inc'; - break; - } - } - - if (!empty($curContext)) { - $line = $curContext; - $curContext = ''; - require CHORA_TEMPLATES . '/diff/hr/empty.inc'; - } - } - } + $view = $injector->createInstance('Horde_View_Base'); + $view->addHelper('Chora_Diff_Helper'); + echo $view->diff($VC->diff($fl, $r1, $r2, array('human' => true, 'num' => $num, 'ws' => $ws))); + echo $view->diffCaption(); } -require CHORA_TEMPLATES . '/diff/hr/footer.inc'; require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/chora/templates/diff/header.inc b/chora/templates/diff/header.inc new file mode 100644 index 000000000..2ebe29da1 --- /dev/null +++ b/chora/templates/diff/header.inc @@ -0,0 +1,40 @@ +
+
+
+ + + + + + + " />
+
+
+
+ + +

+
+ +
+
    +
  • +
  • + +
  • + + + +
  • + +
  • +
  • +, -
  • +
+ abbrev($val['rev'])) ?>: +
+
+ +
+ diff --git a/chora/templates/diff/hr/add.inc b/chora/templates/diff/hr/add.inc deleted file mode 100644 index 2a0908d1e..000000000 --- a/chora/templates/diff/hr/add.inc +++ /dev/null @@ -1,4 +0,0 @@ - -   -
- diff --git a/chora/templates/diff/hr/change.inc b/chora/templates/diff/hr/change.inc deleted file mode 100644 index abbd37a6e..000000000 --- a/chora/templates/diff/hr/change.inc +++ /dev/null @@ -1,20 +0,0 @@ - - - -
- - -   - -   - - - -
- - -   - -   - - diff --git a/chora/templates/diff/hr/empty.inc b/chora/templates/diff/hr/empty.inc deleted file mode 100644 index 99f1b9f47..000000000 --- a/chora/templates/diff/hr/empty.inc +++ /dev/null @@ -1,5 +0,0 @@ - -
-
- - diff --git a/chora/templates/diff/hr/footer.inc b/chora/templates/diff/hr/footer.inc deleted file mode 100644 index 02ee59fa5..000000000 --- a/chora/templates/diff/hr/footer.inc +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/chora/templates/diff/hr/header.inc b/chora/templates/diff/hr/header.inc deleted file mode 100644 index 4f9b92f0f..000000000 --- a/chora/templates/diff/hr/header.inc +++ /dev/null @@ -1,67 +0,0 @@ -
-
-
- - - - - - - " />
-
-
-
- - -

-
- -
-
    -
  • -
  • - -
  • - - - -
  • - -
  • -
  • +, -
  • -
- abbrev($val['rev'])) ?>: -
-
- -
- - - - - - - - - - - diff --git a/chora/templates/diff/hr/nochange.inc b/chora/templates/diff/hr/nochange.inc deleted file mode 100644 index 05b818625..000000000 --- a/chora/templates/diff/hr/nochange.inc +++ /dev/null @@ -1,5 +0,0 @@ - - - diff --git a/chora/templates/diff/hr/remove.inc b/chora/templates/diff/hr/remove.inc deleted file mode 100644 index e6c020a87..000000000 --- a/chora/templates/diff/hr/remove.inc +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/chora/templates/diff/hr/row.inc b/chora/templates/diff/hr/row.inc deleted file mode 100644 index c115de350..000000000 --- a/chora/templates/diff/hr/row.inc +++ /dev/null @@ -1,8 +0,0 @@ - - - - diff --git a/chora/themes/screen.css b/chora/themes/screen.css index 5b287ec96..bb7408c7a 100644 --- a/chora/themes/screen.css +++ b/chora/themes/screen.css @@ -12,7 +12,7 @@ form#repository-picker { /* Options boxes. */ div.options, div.browsefileoptions { - margin: 0 0 8px 5px; + margin: 0 0 8px 0; } div.options div, div.browsefileoptions div { display: -moz-inline-stack; @@ -34,9 +34,9 @@ div.browsefileoptions form { /* Tables. */ table.revlog, table.headerbar, table.browse, table.stats { - width: 99%; + width: 100%; font-size: 100%; - margin: 0 0 8px 5px; + margin: 0 0 8px 0; border-top: 1px solid #ddd; border-left: 1px solid #ddd; } @@ -66,7 +66,7 @@ table.headerbar td { /* History View. */ table.history, p.history { - margin: 0 0 8px 5px; + margin: 0 0 8px 0; border: 1px solid #ddd; } table.history a { @@ -76,7 +76,7 @@ table.history a { /* Individual revision information. */ div.revision_log { - margin: 2px 5px 8px 5px; + margin: 2px 5px 8px 0; border: 1px solid #ddd; padding: 3px; } @@ -95,7 +95,7 @@ h3.revision_log, h3.checkout, h3.file-view-header { background: #e9e9e9; display: inline; font-weight: bold; - margin: 0 0 0 5px; + margin: 0; font-size: 90%; padding: 2px; border: 1px solid #ddd; @@ -185,7 +185,7 @@ ul.singlepsfiles li { /* Checkout, File view */ div.checkout, div.file-view-contents { padding: 3px; - margin: 2px 5px 8px 5px; + margin: 2px 5px 8px 0; border: 1px solid #ddd; } div.file-view { @@ -200,7 +200,7 @@ div.file-view { /* Annotate styles. */ table.annotate { width: 99%; - margin: 0 0 8px 5px; + margin: 0 0 8px 0; border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-bottom: 1px solid #ddd; @@ -224,54 +224,49 @@ table.annotate .logentry { } /* Diff styles. */ -table.hrdiff { - width: 99%; +table.diff { + width: 100%; font-size: 100%; - margin: 0 0 8px 5px; -} -table.hrdiff th { - text-align: left; - width: 50%; + margin: 0 0 8px 0; } -table.hrdiff thead th { +table.diff th { + font-size: 80%; font-weight: bold; - font-size: 110%; - padding: 2px; } -table.hrdiff tbody th { - padding: 2em 1px 1px 1px; - font-size: 80%; + +div.diff { + overflow: auto; } -table.hrdiff .unmodified { + +.diff-unmodified { background: #fff; + color: #666; } -table.hrdiff .added { - background: #9f9; +.diff-added { + background: #afa; } -table.hrdiff .added_empty { +.diff-added-empty { background: #cfc; } -table.hrdiff .modified { - background: #fd9; +.diff-modified { + background: #cdf; } -table.hrdiff .removed { +.diff-removed { background: #f99; } -table.hrdiff .removed_empty { +.diff-removed-empty { background: #fcc; } -table.hrdiff caption { - caption-side: top; + +.diff-caption { text-align: left; - margin: 0 0 8px 5px; + margin: 0 0 8px 0; font-size: 90%; font-weight: bold; padding: 5px; } -table.hrdiff caption img, img.chora-stats { +.diff-caption img { border: 1px solid #000; -} -table.hrdiff caption img { height: 10px; width: 10px; margin-left: 0.5em; @@ -280,7 +275,7 @@ table.hrdiff caption img { div.difflog { padding: 2px; - margin: 5px; + margin: 0; border: 1px solid #ddd; } div.diffclear { @@ -292,6 +287,10 @@ td.browseLocation em { display: block; } +img.chora-stats { + border: 1px solid #000; +} + .historybg { background: #e9e9e9; text-align: center;
- - <?php echo _(" /> - <?php echo _(" /> - <?php echo _(" /> - <?php echo _(" /> -
- -
 
- - - -