Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: megaplanet/megalib
base: 15cdea1d5b
...
head fork: megaplanet/megalib
compare: e9cc07a6e1
  • 3 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
2  Files.php
@@ -304,7 +304,7 @@ function linkInformation($link) {
$realpath = realpath($r['targetPath']) ;
$r['realTargetPath'] = is_string($realpath) ? $realpath : null ;
$r['isBroken'] = !is_string($realpath) ;
- $r['isRealTarget'] = ($r['$targetPath'] === $r['$realTargetPath']) ;
+ $r['isRealTarget'] = ($r['targetPath'] === $r['realTargetPath']) ;
return $r ;
}
}
View
154 SourceCode.php
@@ -0,0 +1,154 @@
+<?php
+require_once 'config/configSourceCode.php';
+require_once 'HTML.php' ;
+require_once 'Strings.php' ;
+
+
+/**
+ * This class provides support for source code analysis, manipulation, display, etc.
+ */
+class SourceCode {
+
+
+ /**
+ * @var String! a unique code that is used in particular as a prefix od html ids
+ * when various source code are to be displayed in the same page. If this code is
+ * not explicitely provided then is is automatically generated.
+ */
+ protected $sourceId ;
+
+ /**
+ * @var an integer used for the automatic generation of sourceIds.
+ * This variable is global and represents the next available number.
+ */
+ private static $nextIdAvailable = 0 ;
+
+ /**
+ * Return a new generated source id
+ * @return String! a new source id
+ */
+ private static function getNewSourceId() {
+ $id = SourceCode::$nextIdAvailable ;
+ SourceCode::$nextIdAvailable = $id+1 ;
+ return "s$id" ;
+ }
+
+ /**
+ * @var String! language string used by the geshi package for highlighting
+ */
+ protected $language ;
+
+ /**
+ * @var String! the source code
+ */
+ protected $source ;
+
+ /**
+ * @var HTML? the html version of the highlighted source code. Computed on demand.
+ */
+ protected $highlighted ;
+ /**
+ * @var SimpleXMLElement? The XML representation of highlighted source code, computed on demand.
+ */
+ protected $highlightedAsSimpleXML ;
+
+ /**
+ * @var GeSHI?
+ */
+ protected $geshi;
+
+
+ /**
+ * @return GeSHI!
+ */
+ protected function getGeSHI() {
+ if (!isset($this->geshi)) {
+ $geshi = new GeSHi($this->source, $this->language);
+ $geshi->set_overall_id($this->sourceId) ;
+ $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS) ;
+ $geshi->enable_classes();
+ $geshi->enable_ids(true);
+ $this->geshi = $geshi ;
+ }
+ return $this->geshi ;
+ }
+
+ public function getRawHTML() {
+ return htmlAsIs($this->source) ;
+ }
+
+ /**
+ *
+ */
+ public function getHighlightingStyle($lines='',$style='background:#FFAAEE;') {
+ $css = $this->getGeSHI()->get_stylesheet();
+ $idprefix='#'.$this->sourceId.'-' ;
+ $ids = $idprefix.implode(','.$idprefix,rangesExpression($lines)) ;
+ if ($ids!='') {
+ $css .= $ids. ' { '.$style.' }' ;
+ }
+ return '<html><head><title>Code</title><style type="text/css"><!--'.$css.'--></style></head>' ;
+ }
+
+ /**
+ * @return HTML?
+ */
+ public function getHighlightedHTML() {
+ if (!isset($this->highlighted)) {
+ $geshi = $this->getGeSHI() ;
+ $html = $geshi->parse_code();
+ $this->highlighted = str_replace('&nbsp;',' ',$html) ;
+ }
+ return $this->highlighted ;
+ }
+
+
+
+ /**
+ * @return SimpleXMLElement
+ */
+ protected function getHighlightedAsSimpleXML() {
+ if (!isset($this->highlightedAsSimpleXML)) {
+ $simpleXML = simplexml_load_string($this->getHighlightedHTML()) ;
+ if ($simpleXML===false) {
+ die('error: HMTL is not valid XML') ;
+ } else {
+ $this->highlightedAsSimpleXML = $simpleXML ;
+ }
+ }
+ return $this->highlightedAsSimpleXML ;
+ }
+
+ /**
+ * @param String $classname
+ * @return List*(SimpleXMLElement) the list of nodes corresponding to the given class
+ */
+ public function getTokensAsElements($classname) {
+ $simpleXML = $this->getHighlightedAsSimpleXML() ;
+ return $simpleXML->xpath('//span[@class="'.$classname.'"]') ;
+ }
+
+ /**
+ * @param unknown_type $classname
+ * @return List*(String!)!
+ */
+ public function getTokensAsTexts($classname) {
+ $elements = $this->getTokensAsElements($classname) ;
+ $texts = array() ;
+ foreach ($elements as $element) {
+ $texts[] = (string) $element ;
+ }
+ return $texts ;
+ }
+
+ public function __construct($text,$language,$sourceid=null) {
+ $this->source = $text ;
+ $this->language = $language ;
+ if (isset($sourceid)) {
+ $this->sourceId=$sourcedid ;
+ } else {
+ $this->sourceId = SourceCode::getNewSourceId() ;
+ }
+ }
+
+}
View
22 Strings.php
@@ -17,6 +17,28 @@ function boolStr($value,$strings="false,true,null") {
: ($value ?$strs[1]:$strs[0]) ;
}
+
+function rangesExpression($expression){
+ $ranges=explode(',',$expression) ;
+ $values = array() ;
+ foreach($ranges as $range) {
+ if ($range!='') {
+ $bounds=explode('-',$range) ;
+ switch (count($bounds)) {
+ case 1:
+ $values[] = intval($bounds[0]);
+ break ;
+ case 2:
+ $values = union($values,range(intval($bounds[0]),intval($bounds[1]))) ;
+ break ;
+ default:
+ }
+ }
+ }
+ sort($values) ;
+ return $values ;
+}
+
function startsWith($haystack, $needle){
return substr($haystack, 0, strlen($needle)) === $needle;
}
View
17 config/configSourceCode.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * COPY AND PASTE the code below into a file named
+ * localConfigXXX.php in this very directory.
+ * Then adapt the constants to your local settings.
+ * Note that the file localConfigXXX are ignored during commit.
+ * -------------------------------------------------------------
+ * Setup for using SourceCode.php
+ * Download the geshi package from http://qbnz.com/highlighter/
+ * Copy the directory at the top level. Otherwise change the
+ * _LIBRARY constant below.
+<?php
+// Path to the package
+define('SRC_GESHI_LIBRARY',__DIR__.'/../../geshi') ;
+ */
+require_once 'localConfigSourceCode.php' ;
+require_once SRC_GESHI_LIBRARY.'/geshi.php' ;
View
39 drafts/SourceCode.php
@@ -1,39 +0,0 @@
-<?php
-require '../../geshi/geshi.php' ;
-
-
-function outputHeader($geshi) {
- echo '<html><head><style type="text/css"><!--';
- $geshi->get_stylesheet();
- echo '--></style></head><body>';
-}
-
-function getElements($xml, $classname) {
- return $xml->xpath('//span[@class="'.$classname.'"]') ;
-}
-
-function getTexts($xml,$classname) {
- $texts = array() ;
- foreach (getElements($xml,$classname) as $element) {
- $texts[] = (string) $element ;
- }
- return $texts ;
-}
-
-$source = file_get_contents('SourceCode.php') ;
-$geshi = new GeSHi($source, 'php');
-$geshi->set_overall_id('code') ;
-$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS) ;
-$geshi->enable_classes();
-$geshi->enable_ids(true);
-outputHeader($geshi) ;
-$geshi->highlight_lines_extra(array(2,6,7)) ;
-$html = $geshi->parse_code();
-$html=str_replace('&nbsp;',' ',$html) ;
-$xml = simplexml_load_string($html) ;
-if ($xml===false) {
- die('error: the HMTL is not valid XML') ;
-}
-echo $html ;
-var_dump(getElements($xml,'re0')) ;
-var_dump(getTexts($xml,'re0')) ;
View
86 tests/Strings.php
@@ -1,86 +0,0 @@
-<?php
-function startsWith($haystack, $needle){
- return substr($haystack, 0, strlen($needle)) === $needle;
-}
-
-function endsWith($haystack, $needle)
-{
- return (substr($haystack, - strlen($needle)) === $needle);
-}
-
-function prefixIfNeeded($str, $prefix) {
- return startsWith($str,$prefix) ? $str : $prefix . $str ;
-}
-
-
-/**
- * Remove comments and replace them by a given string.
- * @param String! $expr The string to clean.
- * @param RegExpr? $commentRegExpr the regular expression corresponding to a comment.
- * Default to C_LINE_COMMENT_REGEXPR for suppressing // comments.
- * @param String? $replacement Replacement string (default to a newline).
- */
-
-define ('LINE_COMMENT_SUFFIX','[^\n]*\n') ;
-define ('SHELL_LINE_COMMENT_REGEXPR','/#'.LINE_COMMENT_SUFFIX.'/') ;
-define ('C_LINE_COMMENT_REGEXPR','/\/\/'.LINE_COMMENT_SUFFIX.'/') ;
-define ('ADA_LINE_COMMENT_REGEXPR','/--'.LINE_COMMENT_SUFFIX.'/') ;
-
-function removeComments($expr,$commentRegExpr=C_LINE_COMMENT_REGEXPR,$replacement="\n") {
- return preg_replace($commentRegExpr,$replacement,$expr) ;
-}
-
-
-function withoutOptionalPrefix($str, $prefix) {
- return startsWith($str,$prefix) ? substr($str,strlen($prefix)) : $str ;
-}
-
-
-function alternativeToDiacritics($textWithAccents) {
- return strtr($text,$_TRANSLATE['alternativeToDiacritics']) ;
-}
-
-
-/**
- * Extract identifiers from a text
- * @param String $text
- * @return
- */
-function extractIds($text) {
- // $regexpr = '/[^a-zA-Z_0-9]+/' ;
- $regexpr = '/[^a-zA-Z_]+/' ;
- $onlyids = trim(preg_replace($regexpr,' ',$text)) ;
- $ids = array_count_values(explode(' ',$onlyids)) ;
- ksort($ids) ;
- return $ids ;
-}
-
-
-/**
- * Split an identifier in its logical segmements.
- * @param String $id
- * @param (Function(String!):String!)? A function to apply on each segment.
- * Could be either a anonymous function or a function name like "strtolower",
- * "strtoupper" or "ucfirst". If null is provided then each segment is left as is.
- * Default to strtolower.
- * left as is.
- * @param $removeNumbers If set to true remove the numeric segments. Default is true.
- * @param $removeUnderscores If set to true remove the underscore segments. Default is true.
- * @return List*(String!*)! The list of segment in the identifier
- */
-function explodeId($id,$fun="strtolower",$removeNumbers=true,$removeUnderscores=true) {
- $numberReplacement = ($removeNumbers ? ' ' : ' $1 ') ;
- $underscoresReplacement = ($removeUnderscores ? ' ' : ' $1 ') ;
- $idnew = preg_replace( '/([a-z])([A-Z])/', '$1 $2', $id );
- $idnew = preg_replace( '/(_+)/', $underscoresReplacement, $idnew) ;
- $idnew = preg_replace( '/([0-9]+)/',$numberReplacement, $idnew) ;
- $idnew = preg_replace( '/([A-Z]+)([A-Z][a-z]+)/', "$1 $2", $idnew );
- $idnew = trim(preg_replace('/ /',' ',$idnew)) ;
- $segments = explode(' ',$idnew) ;
- if (isset($fun)) {
- $segments = array_map($fun,$segments) ;
- }
- return $segments ;
-}
-
-
View
39 tests/testFiles_link.php
@@ -1,16 +1,51 @@
<?php
error_reporting(E_ALL);
define('DEBUG',12) ;
+
+echo "parameters: ?dir=<directory>&expr=<expr>" ;
+
require_once '../Files.php' ;
$dir=isset($_GET['dir'])?$_GET['dir']:'.' ;
$expr=isset($_GET['expr'])?$_GET['expr']:'test' ;
+echo "<h2>Testing of links $dir</h2>" ;
+
+echo "<li>create a file in $dir/file1 ... " ;
+$r=file_put_contents("$dir/file1","toto") ;
+echo ($r===false ? "failed" : $r." bytes written").'</li>' ;
+
+echo "<li>create a file in $dir/file2 ... " ;
+$r=file_put_contents("$dir/file2","toto") ;
+echo ($r===false ? "failed" : $r." bytes written").'</li>' ;
+
+echo "<li>create a link $dir/link1 -> $dir/file1 ... " ;
+echo boolStr(symlink("$dir/file1","$dir/link1")).'</li>' ;
+
+echo "<li>removing the file $dir/file1 so that link1 will be broken ... " ;
+echo boolStr(unlink("$dir/file1")).'</li>' ;
-echo "<h2>Listing all broken links in $dir</h2>" ;
-$links=listAllLinksWithInfo($dir,true);
+echo "<li>create a link ","$dir/link2 -> $dir/file2 ... " ;
+echo boolStr(symlink("$dir/file2","$dir/link2")).'</li>' ;
+
+echo "<li>Listing all links in $dir ... " ;
+$links=listAllLinksWithInfo($dir);
+echo count($links).' links found</li>';
+echo "<ul>" ;
foreach($links as $link) {
echo "<li>".formatLinkInformation($link).'</li>' ;
}
+echo "</ul>" ;
+
+
+echo "<li>removing $dir/file2 ... " ;
+echo boolStr(unlink("$dir/file2")) ;
+echo ", $dir/link1 " ;
+echo boolStr(unlink("$dir/link1")) ;
+echo ", $dir/link2 " ;
+echo boolStr(unlink("$dir/link2")) ;
+
+
+
echo "<h2>Grep in $expr directory $dir</h2>" ;
$items=grepDirectory($dir,$expr) ;
View
18 tests/testSourceCode.php
@@ -0,0 +1,18 @@
+<?php
+require_once '../SourceCode.php' ;
+$text = file_get_contents('../SourceCode.php') ;
+$source = new SourceCode($text,'php') ;
+//echo $source->getRawHTML() ;
+echo $source->getHighlightingStyle('1-62,72-123','font-size:4pt ; background:#eeeeaa ; ') ;
+
+echo $source->getHighlightedHTML() ;
+
+//$geshi->highlight_lines_extra(array(2,6,7)) ;
+
+
+
+
+// var_dump(getElements($xml,'re0')) ;
+var_dump($source->getTokensAsTexts('br0')) ;
+
+echo '<h2>End of tests</h2>' ;
View
22 tests/testStrings.php
@@ -1,12 +1,30 @@
<?php
+define('DEBUG',10) ;
require_once '../Strings.php' ;
require_once '../Files.php' ;
-define('DIR','c:/RECHERCHE/ARTICLES/MODELS2012/ATL') ;
+define('DIR','c:/RECHERCHE/ARTICLES/MODELS2012/src') ;
-//var_dump(explodeId('todldELEMETSToto_titi_TIT239ITTotoTOT__','strtolower')) ;
+testRangesExpression() ;
+//var_dump(explodeId('todldELEMETSToto_titi_TIT239ITTotoTOT__','strtolower')) ;
+function testRangesExpression() {
+ echo '<h2>Testing rangesExpression</h2>' ;
+ $expressions = array(
+ "",
+ "1",
+ "1-4",
+ "1,2,5",
+ "9-6",
+ "1-3,2-5,12-20,9-8") ;
+ foreach ($expressions as $expression) {
+ echo '<li>rangeExpression("<b>'.$expression.'</b>") = array(' ;
+ echo implode(',',rangesExpression($expression)).')' ;
+ echo "</li>" ;
+ }
+}
+echo "<h1>identifier extraction should be reworked</h1>" ;
$ids=array() ;
foreach(listAllFileNames(DIR) as $filename) {
if (isFile($filename)) {

No commit comments for this range

Something went wrong with that request. Please try again.