Skip to content

Commit

Permalink
Release 1.0.3: accept drag/drop of .EPUB files and process all files …
Browse files Browse the repository at this point in the history
…within the EPUB. Added a LangTag DropScript which finds a lang and/or xml:lang attribute on <body> and moves them to <html>. Added more patterns to UpdateClass DropScript.

#17
  • Loading branch information
zwettemaan committed May 3, 2019
1 parent ec7daed commit 352c7a5
Show file tree
Hide file tree
Showing 19 changed files with 1,790 additions and 49 deletions.
File renamed without changes.
27 changes: 19 additions & 8 deletions DropScripts/AutoTitle/AutoTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ function processDOM($config, &$isModified, &$dom) {

}

function preProcessFile($config, &$isModified, &$fileContents) {

}

function postProcessFile($config, &$isModified, &$fileContents) {

}

// -- AUTO-GENERATED CODE BELOW. DO NOT EDIT BELOW

// Any code below this line will be auto-updated from the DropScriptTemplate.php. If this file is not
Expand All @@ -118,33 +126,36 @@ function main($droppedFile) {
break;
}

$isModified = false;

$fileContents = file_get_contents($droppedFile);

preProcessFile($config, $isModified, $fileContents);

$dom = new DomDocument();

if ($config["ignoreDOMParserErrors"]) {
libxml_use_internal_errors(true);
}

$dom->loadHTMLFile($droppedFile);
$dom->loadHTML($fileContents);

if ($config["ignoreDOMParserErrors"]) {
libxml_clear_errors();
}

$isModified = false;

processDOM($config, $isModified, $dom);

// ...
//
$output = $dom->saveHTML();

postProcessFile($config, $isModified, $output);

if (! $isModified) {
break;
}

makeBackup($config, $droppedFile);

$output = $dom->saveHTML();

writeFileContents($droppedFile, $output);
}
catch (Exception $e) {
Expand All @@ -165,7 +176,7 @@ function defaultConfig() {

$config = [];

$config["acceptFileNameExtensions"] = [ "html", "htm", "xhtml", "xht" ];
$config["acceptFileNameExtensions"] = [ "html", "htm", "xhtml" ];
$config["backupFileNameExtension"] = "old";
$config["maxBackupCount"] = 5;
$config["logLevel"] = LOG_NONE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"acceptFileNameExtensions": [ "html", "htm", "xhtml", "xht" ],
"acceptFileNameExtensions": [ "html", "htm", "xhtml" ],
"backupFileNameExtension": "old",
"maxBackupCount": 5
}
35 changes: 25 additions & 10 deletions DropScripts/DropScriptTemplate/DropScriptTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ function processDOM($config, &$isModified, &$dom) {
// Do something (e.g. manipulate DOM)
// Set $isModified to true if you changed the DOM
// The $config might contain settings that change the behavior of your script.
//

echo "Accepted\n" . readFileContents($droppedFile); // something to do as an example
}

function preProcessFile($config, &$isModified, &$fileContents) {

// If you reverse the changes made here in postProcessFile, you should
// not set isModified

}

function postProcessFile($config, &$isModified, &$fileContents) {

// If this reverses the changes made in preProcessFile, you should
// not set isModified

}

// -- AUTO-GENERATED CODE BELOW. DO NOT EDIT BELOW
Expand All @@ -34,33 +46,36 @@ function main($droppedFile) {
break;
}

$isModified = false;

$fileContents = file_get_contents($droppedFile);

preProcessFile($config, $isModified, $fileContents);

$dom = new DomDocument();

if ($config["ignoreDOMParserErrors"]) {
libxml_use_internal_errors(true);
}

$dom->loadHTMLFile($droppedFile);
$dom->loadHTML($fileContents);

if ($config["ignoreDOMParserErrors"]) {
libxml_clear_errors();
}

$isModified = false;

processDOM($config, $isModified, $dom);

// ...
//
$output = $dom->saveHTML();

postProcessFile($config, $isModified, $output);

if (! $isModified) {
break;
}

makeBackup($config, $droppedFile);

$output = $dom->saveHTML();

writeFileContents($droppedFile, $output);
}
catch (Exception $e) {
Expand All @@ -81,7 +96,7 @@ function defaultConfig() {

$config = [];

$config["acceptFileNameExtensions"] = [ "html", "htm", "xhtml", "xht" ];
$config["acceptFileNameExtensions"] = [ "html", "htm", "xhtml" ];
$config["backupFileNameExtension"] = "old";
$config["maxBackupCount"] = 5;
$config["logLevel"] = LOG_NONE;
Expand Down
67 changes: 67 additions & 0 deletions DropScripts/LangTag/LangTag.ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# LangTag.php

## What it does

AutoTitle.php is a command-line PHP script which can process an HTML file.

It will look for the &lt;body&gt; and &lt;html&gt; tags.

It will move any attributes lang=... or xml:lang=... from the &lt;body&gt; tag up to the &lt;html&gt; tag

## Configuration

LangTag.php will look for a file LangTag.config.txt nearby. In that file you can set
or override configuration options.

The names of these options are case-sensitive.

### acceptFileNameExtensions

Optional. The script will ignore any files whose file name does not end in one of the listed extensions

The default when this is omitted is:

"acceptFileNameExtensions": [ "html", "htm", "xhtml" ]

### backupFileNameExtension

Optional. The file name extension to use for backup files

The default when this is omitted is:

"backupFileNameExtension": "old"

### logLevel

Set to 0, 1, 2, 3, or 4. Depending on the number, more or less logging information will be
provided. Useful for diagnostics when the script does not seem to work correctly.

The default when this is omitted is:

"logLevel": 0

### maxBackupCount

Optional. How many backup files to keep.

The default when this is omitted is:

"maxBackupCount": 5

If maxBackupCount is 0, no backups are made

If maxBackupCount is greater than zero the backups will be

SomeFile.xhtml ->
SomeFile.xhtml.old
SomeFile.xhtml_1.old
SomeFile.xhtml_2.old
...
SomeFile.xhtml_5.old

The SomeFile.xhtml.old is special: once made, it is never overwritten. It
stores the content of the original file from before you ran the script for the
first time.

SomeFile.xhtml_1.old, SomeFile.xhtml_2.old... rotate. The _1 file is the latest,
the _5 file is the oldest.
2 changes: 2 additions & 0 deletions DropScripts/LangTag/LangTag.config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading

0 comments on commit 352c7a5

Please sign in to comment.