Browse Source

Add commands insertHtml, replaceHtml ; variable expansion mechanism - first real-life usage to generate footers

master
Thierry 4 years ago
parent
commit
ff40a19efa
16 changed files with 333 additions and 39 deletions
  1. +5
    -0
      .gitignore
  2. +9
    -0
      docs/index.html
  3. +0
    -2
      sites/tig12.net/commands/addfooter.yml
  4. +11
    -0
      sites/tig12.net/commands/insertFooter.yml
  5. +8
    -0
      sites/tig12.net/commands/replaceFooter.yml
  6. +5
    -1
      sites/tig12.net/config.yml
  7. +1
    -0
      src/commands/common/README
  8. +28
    -0
      src/commands/common/checkSiteConfig.php
  9. +62
    -0
      src/commands/common/expandVariables.php
  10. +89
    -0
      src/commands/insertHtml.php
  11. +0
    -15
      src/commands/makenav.php
  12. +0
    -15
      src/commands/parsepage.php
  13. +96
    -0
      src/commands/replaceHtml.php
  14. +4
    -2
      src/lib/dirs/jth_rscandir.php
  15. +5
    -2
      src/lib/patterns/command/Command.php
  16. +10
    -2
      src/run-tigsite.php

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@

z.*
*~
y.build


+ 9
- 0
docs/index.html View File

@@ -0,0 +1,9 @@




<h2>Site config.yml</h2>
- 'location' : absolute path to directory containing the files to process.
- 'exclude' (optional) : array of strings containing regular expressions
of relative paths to files to exclude from processing.
Defaults to an empty array

+ 0
- 2
sites/tig12.net/commands/addfooter.yml View File

@@ -1,2 +0,0 @@
files: /
footer:

+ 11
- 0
sites/tig12.net/commands/insertFooter.yml View File

@@ -0,0 +1,11 @@

#
# Executed only once
#
commandClass: insertHtml
before: '</body>'
insert-string: |
<footer class="footer">
</footer>
exclude:
- index.html

+ 8
- 0
sites/tig12.net/commands/replaceFooter.yml View File

@@ -0,0 +1,8 @@


commandClass: replaceHtml
before: '<footer class="footer">'
after: '</footer>'
replacement-file: static/html/footer.html
#exclude:
# - index.html

+ 5
- 1
sites/tig12.net/config.yml View File

@@ -1,5 +1,9 @@
location: /home/thierry/dev/tig12/tig12.net/web
exclude:
- '*/z.*'
- .git
- .gitignore
- static
- x-templates
- maths/categories/milewski
- maths/grothenuls

+ 1
- 0
src/commands/common/README View File

@@ -0,0 +1 @@
Contains code shared by several commands

+ 28
- 0
src/commands/common/checkSiteConfig.php View File

@@ -0,0 +1,28 @@
<?php
/******************************************************************************
Checks the correctness of a site configuration file
@license GPL
@history 2019-02-18 12:01:41+01:00 Thierry Graff, Creation
********************************************************************************/

class checkSiteConfig{

// ******************************************************
/**
@param $config Configuration contained in config.yml of a site, in an array
@return An array containing the configuration with default values filled
@throws Exception if a directive is missing or invalid
**/
public static function check($config){
if(!isset($config['location'])){
throw new Exception("Missing \$config['site']['location']");
}
if(!isset($config['exclude'])){
$config['exclude'] = [];
}
return $config;
}
}// end class


+ 62
- 0
src/commands/common/expandVariables.php View File

@@ -0,0 +1,62 @@
<?php
/******************************************************************************

Used for commands that need to deal with strings that depend on the currently processed html file.
A special syntax permits to replace strings located between {{ and }} by strings computed by this class

Supported strings :
- {{path-to-root}} : relative html path between file currently processed and root of the site.

@license GPL
@history 2019-02-18 16:15:29+01:00 : Creation
********************************************************************************/

class expandVariables{
// ******************************************************
/**
@param $subject ThePiece of html that may contain strings to expanded
@param $params Depend on the strings that need to be expanded
See particular functions to have the list of required parameters.
**/
public static function expand($subject, $params){
preg_match_all('/\{\{(.*?)\}\}/sm', $subject, $matches);
$translate = [];
for($i=0; $i < count($matches[0]); $i++){
switch($matches[1][$i]){
case 'path-to-root' :
if(isset($translate['{{path-to-root}}'])){
continue;
}
$translate['{{path-to-root}}'] = self::path_to_root($params);
break;
}
}
return strtr($subject, $translate);
}
// ******************************************************
/**
Returns the replacement string for {{path-to-root}} variable
@param $params Associative array with the following keys :
- 'root-dir'
- 'current-file'
**/
private static function path_to_root($params){
if(!isset($params['root-dir'])){
throw new Exception("Missing \$params['root-dir']");
}
if(!isset($params['current-file'])){
throw new Exception("Missing \$params['current-file']");
}
if(strpos($params['root-dir'], $params['current-file']) != 0){
throw new Exception("Incoherence between \$params['root-dir'] and \$params['current-file']");
}
$relative = str_replace($params['root-dir'], '', $params['current-file']);
$parts = explode('/', $relative);
$n = count($parts) - 2; // -2 because -1 for first / and -1 for last part of the path
return str_repeat('../', $n);
}
}// end class

+ 89
- 0
src/commands/insertHtml.php View File

@@ -0,0 +1,89 @@
<?php
/******************************************************************************
Inserts existing html code in a page.

@license GPL
@history 2019-02-18 12:13:18+01:00, Thierry Graff : Creation
********************************************************************************/
class insertHtml implements Command {
/**
Inserts existing html code in a page.
Restrictions :
- In the existing pages, the html code specified by $params['before'] or $params['after'] must exist.
must exist and be unique.
@param $params Associative array that MUST contain the following keys :
- 'site' (required) : associative array ; see format in docs/
- 'command' (required) : associative aray with the following keys :
- 'before' or 'after' : html piece of code to mark the place where the new html must be inserted.
@throws Exception in case of bad parameter
@todo Add parameters "config-file" and "command-file" (only useful for messages in parameter checking)
**/
public static function execute($params){
//
// check parameters
//
$params['site'] = checkSiteConfig::check($params['site']);
if(!isset($params['command']['before']) && !isset($params['command']['after'])){
throw new Exception("\$params['command'] must contain either 'before' or 'after'");
}
if(isset($params['command']['before']) && isset($params['command']['after'])){
throw new Exception("\$params['command'] cannot contain both 'before' and 'after'");
}
if(!isset($params['command']['insert-file']) && !isset($params['command']['insert-string'])){
throw new Exception("\$params['command'] must contain either 'insert-file' or 'insert-string'");
}
if(isset($params['command']['insert-file']) && isset($params['command']['insert-string'])){
throw new Exception("\$params['command'] cannot contain both 'insert-file' and 'insert-string'");
}
if(!isset($params['command']['exclude'])){
$params['command']['exclude'] = [];
}
//
// do the job
//
if(isset($params['command']['insert-file'])){
$insert = file_get_contents($params['site']['location'] . DS . $params['command']['insert-file']);
}
else{
$insert = $params['command']['insert-string'];
}
$excludes = [];
foreach($params['site']['exclude'] as $exclude){
$excludes[] = $params['site']['location'] . DS . $exclude;
}
foreach($params['command']['exclude'] as $exclude){
$excludes[] = $params['site']['location'] . DS . $exclude;
}
$rscandirParams = [
'include' => '*.html',
'exclude' => $excludes,
'return-dirs' => false,
];
$files = jth_rscandir::rscandir($params['site']['location'], $rscandirParams);
if(isset($params['command']['before'])){
$find = $params['command']['before'];
$replace = $insert . $params['command']['before'];
}
else{
$find = $params['command']['after'];
$replace = $params['command']['after'] . $insert;
}
$N = count($files);
for($i=0; $i < $N; $i++){
echo "processing {$files[$i]}\n";
$old = file_get_contents($files[$i]);
$replace2 = expandVariables::expand($replace, ['root-dir' => $params['site']['location'], 'current-file' => $files[$i]]);
$new = str_replace($find, $replace2, $old);
file_put_contents($files[$i], $new);
}
}
}// end class

+ 0
- 15
src/commands/makenav.php View File

@@ -1,15 +0,0 @@
<?php
/******************************************************************************
Generates navigation links for a part of a site.

@license GPL
@author Thierry Graff
@history : 2019-02-02 02:47:43+01:00, Creation
********************************************************************************/

public class makenav implements Command{
public function execute(){
}
}// end class

+ 0
- 15
src/commands/parsepage.php View File

@@ -1,15 +0,0 @@
<?php
/******************************************************************************
Parses a html page and outputs an array.

@license GPL
@author Thierry Graff
@history : 2019-02-02 02:47:43+01:00, Creation
********************************************************************************/

public class parsepage implements Command{
public function execute($params){
}
}// end class

+ 96
- 0
src/commands/replaceHtml.php View File

@@ -0,0 +1,96 @@
<?php
/******************************************************************************
Replaces existing html code in a page by a new html string.
@license GPL
@history 2019-02-02 02:47:43+01:00, Thierry Graff : Creation
********************************************************************************/
class replaceHtml implements Command {
/**
Replaces existing html code in a page by a new html string.
Restrictions :
- In the existing pages, the html code contained in $params['before'] and $params['after']
must exist and be unique.
@param $params Associative array that MUST contain the following keys :
- 'site' (required) : associative array ; see format in docs/
- 'command' (required) : associative aray with the following keys :
- 'before' and 'after' (required) : html piece of code surrounding the html replaced by this function.
- 'replacement-file' : relative path to the file containing the a file containing
the new html code to insert between 'before' and 'after'.
- 'replacement-string' : string containing the new html code to insert between 'before' and 'after'.
NOTE : 'command' must contain 'replacement-file' or 'replacement-string' but not both.
@throws Exception in case of bad parameter
@todo Add parameters "config-file" and "command-file" (only useful for messages in parameter checking)
@todo Maybe add a parameter to specify the number of occurences replaced
@todo Maybe add a parameter to specify which occurences are replaced
**/
public static function execute($params){
//
// check parameters
//
$params['site'] = checkSiteConfig::check($params['site']);
if(!isset($params['command']['before'])){
throw new Exception("Missing \$params['command']['before']");
}
if(!isset($params['command']['after'])){
throw new Exception("Missing \$params['command']['after']");
}
if(!isset($params['command']['replacement-file']) && !isset($params['command']['replacement-string'])){
throw new Exception("\$params['command'] must contain either 'replacement-file' or 'replacement-string'");
}
if(isset($params['command']['replacement-file']) && isset($params['command']['replacement-string'])){
throw new Exception("\$params['command'] cannot contain both 'replacement-file' and 'replacement-string'");
}
if(!isset($params['command']['exclude'])){
$params['command']['exclude'] = [];
}
//
// do the job
//
if(isset($params['command']['replacement-file'])){
$replace = file_get_contents($params['site']['location'] . DS . $params['command']['replacement-file']);
}
else{
$replace = $params['command']['replacement-string'];
}
$replace = $params['command']['before'] . $replace . $params['command']['after'];
$excludes = [];
foreach($params['site']['exclude'] as $exclude){
$excludes[] = $params['site']['location'] . DS . $exclude;
}
foreach($params['command']['exclude'] as $exclude){
$excludes[] = $params['site']['location'] . DS . $exclude;
}
$rscandirParams = [
'include' => '*.html',
'exclude' => $excludes,
'return-dirs' => false,
];
$files = jth_rscandir::rscandir($params['site']['location'], $rscandirParams);
$pattern = '#' . preg_quote($params['command']['before']) . '(.*?)' . preg_quote($params['command']['after']) . '#sm';
$N = count($files);
for($i=0; $i < $N; $i++){
echo "processing {$files[$i]}\n";
$subject = file_get_contents($files[$i]);
$replace2 = expandVariables::expand($replace, ['root-dir' => $params['site']['location'], 'current-file' => $files[$i]]);
//echo "$replace2\n";
//continue;
$new = preg_replace($pattern, $replace2, $subject, -1, $count);
if($count == 0){
continue;
}
//echo "$new\n"; exit;
file_put_contents($files[$i], $new);
}
}
}// end class

+ 4
- 2
src/lib/dirs/jth_rscandir.php View File

@@ -104,13 +104,15 @@ class jth_rscandir{
$keep = false;
$excluded = false;
for($i=0; $i < count($params['include']); $i++){
if(fnmatch($params['include'][$i], $basename)){
//if(fnmatch($params['include'][$i], $basename)){
if(fnmatch($params['include'][$i], $current)){
$keep = true;
break;
}
}
for($i=0; $i < count($params['exclude']); $i++){
if(fnmatch($params['exclude'][$i], $basename)){
//if(fnmatch($params['exclude'][$i], $basename)){
if(fnmatch($params['exclude'][$i], $current)){
$keep = false;
$excluded = true;
break;


+ 5
- 2
src/lib/patterns/command/Command.php View File

@@ -1,5 +1,8 @@
<?php

public interface Command{
function execute();
/**
Interface to implement Command design pattern.
**/
interface Command{
public static function execute($params);
}

+ 10
- 2
src/run-tigsite.php View File

@@ -66,11 +66,19 @@ $config = [];
$config['site'] = jthYAML::parse($siteConfigFile);
$config['command'] = jthYAML::parse($commandFile);

echo "\n<pre>"; print_r($config); echo "</pre>\n";
if(!isset($config['command']['commandClass'])){
echo "Missing entry 'commandClass' in $commandFile\n";
exit;
}

exit;
if(!class_exists($config['command']['commandClass'])){
echo "Entry 'commandClass' does not correspond to an existing command class in $commandFile\n";
exit;
}

//echo "\n<pre>"; print_r($config); echo "</pre>\n"; exit;
try{
$config['command']['commandClass']:: execute($config);
}
catch(Exception $e){
echo 'Exception : ' . $e->getMessage() . "\n";


Loading…
Cancel
Save