Browse Source

Add page config and used it to add option 'replacement-directive' to

replaceHtml
master
Thierry 3 years ago
parent
commit
59aa1b7f06
10 changed files with 142 additions and 37 deletions
  1. +17
    -0
      docs/index.html
  2. +0
    -8
      sites/tig12.net/commands/replace-sidebar-maths.yml
  3. +8
    -0
      sites/tig12.net/commands/replace-sidebar-right.yml
  4. +2
    -1
      sites/tig12.net/config.yml
  5. +46
    -0
      src/commands/common/PageConfig.php
  6. +5
    -4
      src/commands/common/SiteConfig.php
  7. +3
    -2
      src/commands/common/expandVariables.php
  8. +1
    -1
      src/commands/insertHtml.php
  9. +60
    -20
      src/commands/replaceHtml.php
  10. +0
    -1
      src/run-tigsite.php

+ 17
- 0
docs/index.html View File

@@ -138,6 +138,23 @@ See code documentation in <code>src/commands/insertHtml.php</code>.
<h3>replaceHtml</h3>
See code documentation in <code>src/commands/replaceHtml.php</code>.


<!-- ********************************************************************************* -->
<h2>Page configuration</h2>
Each html page of the site may contain its own configuration, used by some commands.
<br>Page configuration is a piece of yaml included in a html comment, typically located in the <code>&lt;head></code> part of the page
<br>This yaml must contain a directive <code>tigsite</code> which contains the configuration directives.
<br>
<br>Example :
<pre>
&lt;head>
&lt;!--
tigsite:
sidebar-right: genealogy/sidebar-genealogy.fr.html
-->
&lt;/head>
</pre>

</article>

</body>


+ 0
- 8
sites/tig12.net/commands/replace-sidebar-maths.yml View File

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


commandClass: replaceHtml
before: '<aside class="right">'
after: '</aside>'
replacement-file: maths/sidebar-maths.html
#exclude:
# - index.html

+ 8
- 0
sites/tig12.net/commands/replace-sidebar-right.yml View File

@@ -0,0 +1,8 @@
#
# Command to replace the right sidebar
# Uses the page configuration 'sidebar-right' directive
#
commandClass: replaceHtml
before: '<aside class="right">'
after: '</aside>'
replacement-directive: sidebar-right

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

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

+ 46
- 0
src/commands/common/PageConfig.php View File

@@ -0,0 +1,46 @@
<?php
/******************************************************************************
Page configuration management.
Page configuration is a piece of yaml included in a html comment
that must contain a 'tigsite' (self::MARKER) directive.
@license GPL
@history 2019-03-26 12:17:29+01:00 Thierry Graff, Creation
********************************************************************************/

class PageConfig{

/** String used to identify the page configuration in a html comment **/
const MARKER = 'tigsite';
// ******************************************************
/**
Computes a page configuration : extracts the yaml from the page, checks the syntax, fills default values and returns a correct config.
If the page does not contain configuration, returns an empty array.
@param $page Absolute path to a html file that may contain a configuration
@return An array containing the configuration with default values filled
@throws Exception if a required directive is missing or invalid.
**/
public static function compute($page){
$content = file_get_contents($page);
$p = '#<!--\s*?\n(\s*' . self::MARKER . '\:.*?)-->#sm';
preg_match($p, $content, $m);
if(count($m) == 0){
return []; // no page configuration found.
}
// eliminate useless white spaces
// that may exist if the yaml embedded in the page is tabulated
$pos = strpos($m[1], self::MARKER);
$lines = explode(PHP_EOL, $m[1]);
$config = '';
foreach($lines as $line){
if(trim($line) == ''){
continue;
}
$config .= substr($line, $pos) . PHP_EOL;
}
$yaml = jthYAML::parse($config);
return $yaml[self::MARKER];
}
}// end class

src/commands/common/checkSiteConfig.php → src/commands/common/SiteConfig.php View File

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

class checkSiteConfig{
class SiteConfig{

// ******************************************************
/**
Computes a site configuration : checks the syntax, fills default values and returns a correct config.
@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
@throws Exception if a required directive is missing or invalid.
**/
public static function check($config){
public static function compute($config){
if(!isset($config['location'])){
throw new Exception("Missing \$config['site']['location']");
}

+ 3
- 2
src/commands/common/expandVariables.php View File

@@ -15,7 +15,7 @@ class expandVariables{
// ******************************************************
/**
@param $subject ThePiece of html that may contain strings to expanded
@param $subject The piece 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.
**/
@@ -56,7 +56,8 @@ class expandVariables{
$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);
$tmp = str_repeat('../', $n);
return substr($tmp, 0, -1); // remove last '/'
}
}// end class

+ 1
- 1
src/commands/insertHtml.php View File

@@ -25,7 +25,7 @@ class insertHtml implements Command {
//
// check parameters
//
$params['site'] = checkSiteConfig::check($params['site']);
$params['site'] = SiteConfig::compute($params['site']);
if(!isset($params['command']['before']) && !isset($params['command']['after'])){
throw new Exception("\$params['command'] must contain either 'before' or 'after'");


+ 60
- 20
src/commands/replaceHtml.php View File

@@ -14,13 +14,21 @@ class replaceHtml implements Command {
must exist and be unique.
@param $params Associative array that MUST contain the following keys :
- 'site' (required) : associative array ; see format in docs/
- 'site' (required) : associative array corresponding to global site configuration
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.
- 'before' and 'after' (required) : html pieces of code surrounding the html replaced by this function.
- 'replacement-file' : relative path to the 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'.
- 'replacement-directive' : string containing the directive of a page configuration.
This directive indicates the path to a file containing
the new html code to insert between 'before' and 'after'.
NOTE : 'command' must contain one and only one of
'replacement-file' or 'replacement-string' or 'replacement-directive'
- 'exclude' : array of files that must not be concerned by replacement.
@throws Exception in case of bad parameter
@@ -32,7 +40,7 @@ class replaceHtml implements Command {
//
// check parameters
//
$params['site'] = checkSiteConfig::check($params['site']);
$params['site'] = SiteConfig::compute($params['site']);
if(!isset($params['command']['before'])){
throw new Exception("Missing \$params['command']['before']");
@@ -40,25 +48,36 @@ class replaceHtml implements Command {
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'");
$b1 = isset($params['command']['replacement-file']);
$b2 = isset($params['command']['replacement-string']);
$b3 = isset($params['command']['replacement-directive']);
if(!$b1 && !$b2 && !$b3){
throw new Exception("\$params['command'] must contain either 'replacement-file' or 'replacement-string' or 'replacement-directive'");
}
if(isset($params['command']['replacement-file']) && isset($params['command']['replacement-string'])){
if($b1 && $b2){
throw new Exception("\$params['command'] cannot contain both 'replacement-file' and 'replacement-string'");
}
if($b1 && $b3){
throw new Exception("\$params['command'] cannot contain both 'replacement-file' and 'replacement-directive'");
}
if($b2 && $b3){
throw new Exception("\$params['command'] cannot contain both 'replacement-string' and 'replacement-directive'");
}
if(!isset($params['command']['exclude'])){
$params['command']['exclude'] = [];
}
//
// do the job
// prepare variables
//
if(isset($params['command']['replacement-file'])){
if($b1){
$replace = file_get_contents($params['site']['location'] . DS . $params['command']['replacement-file']);
}
else{
else if($b2){
$replace = $params['command']['replacement-string'];
}
$replace = $params['command']['before'] . $replace . $params['command']['after'];
if(!$b3){
$replace = $params['command']['before'] . $replace . $params['command']['after'];
}
$excludes = [];
foreach($params['site']['exclude'] as $exclude){
@@ -76,17 +95,38 @@ class replaceHtml implements Command {
];
$files = jth_rscandir::rscandir($params['site']['location'], $rscandirParams);
//
// perform replacement
//
$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]]);
foreach($files as $file){
echo "processing $file\n";
$subject = file_get_contents($file);
if($b3){
// $replace must be computed for each page
$pageConfig = PageConfig::compute($file);
if(!isset($pageConfig[$params['command']['replacement-directive']])){
// replacement-directive does not exist for this page, no need to replace
continue;
}
$replacementDirective = $pageConfig[$params['command']['replacement-directive']];
$replacementFile = $params['site']['location'] . DS . $replacementDirective;
if(!is_file($replacementFile)){
$msg = "Bad value for '$replacementDirective' : \"$replacementFile\""
. "\n in file $replacementFile";
throw new Exception($msg);
}
$replace = file_get_contents($replacementFile);
$replace = $params['command']['before'] . $replace . $params['command']['after'];
}
$replace2 = expandVariables::expand($replace, ['root-dir' => $params['site']['location'], 'current-file' => $file]);
// echo "\n"; print_r($replace2); echo "\n";
// continue;
$new = preg_replace($pattern, $replace2, $subject, -1, $count);
if($count == 0){
continue;
}
file_put_contents($files[$i], $new);
file_put_contents($file, $new);
}
}


+ 0
- 1
src/run-tigsite.php View File

@@ -76,7 +76,6 @@ if(!class_exists($config['command']['commandClass'])){
exit;
}

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


Loading…
Cancel
Save