1
0
mirror of https://github.com/php/phd.git synced 2026-03-23 22:52:05 +01:00

New format EnhancedCHM - This format is the same as the CHM format but that it also includes usernotes.

Tested with the normal stylesheet as well as the new stylesheets (reset.css, theme.css and doc.css).
As the usernotes are compressed using BZip2, you will need bzip2.exe to decompress the file from php.net
The source from http://bzip.org/downloads.html compiled very easily using MSVC++2008 Express Edition, though the manifest file isn't packed into the binary (I have a patch for that).
Resultant CHM file is 26MB rather than 10MB.
This commit is contained in:
Richard Quadling
2011-02-18 11:47:44 +00:00
parent 08c16e8e95
commit eefdee2781
4 changed files with 180 additions and 4 deletions

View File

@@ -44,6 +44,7 @@
Prepare CHM rendering to use the new CSS rules (Richard Quadling)
Disply a message when loading an external stylesheet (Richard Quadling)
Incorporate stylesheet names supplied at the command line into the CHM file (Richard Quadling)
New output format EnhancedCHM - the same as CHM but with the User Notes (Requires bzip2.exe) (Richard Quadling)
</notes>
<contents>
@@ -67,6 +68,7 @@
<file name="Epub.php" role="php"/>
<file name="README" role="doc"/>
<file name="HOWTO.GENERATE_CHM" role="doc"/>
<file name="EnhancedCHM.php" role="php"/>
</dir>
</dir>
</dir>

View File

@@ -199,7 +199,7 @@ class Package_PHP_CHM extends Package_PHP_ChunkedXHTML
public function __construct() {
parent::__construct();
$this->registerFormatName("PHP-CHM");
$this->registerFormatName("PHP-CHM");
}
public function __destruct() {
@@ -247,7 +247,7 @@ class Package_PHP_CHM extends Package_PHP_ChunkedXHTML
$stylesheet = $this->fetchStylesheet() . PHP_EOL;
}
file_put_contents($this->outputdir . "style.css", $stylesheet . 'body { padding : 3px;}');
file_put_contents($this->outputdir . "style.css", $stylesheet . 'body {padding : 3px;}' . PHP_EOL . '#usernotes {margin-left : inherit;}' . PHP_EOL);
self::headerChm();
break;
@@ -402,12 +402,12 @@ res' . DIRECTORY_SEPARATOR . 'style.css
$header = parent::header($id);
$patterns = array(
'/(.*)(\r|\n|\r\n|\n\r)(.*)<\/head>/', // Add CSS link to <head>
'/(.*)(\r|\n|\r\n|\n\r)(.*)<\/head>/', // Add CSS link and <meta http-equiv="X-UA-Compatible" content="IE=edge" /> to <head>.
'/(<body)/', // Add 'docs' class to body - the new CSS styling requires a parent of class 'docs'.
);
$replacements = array(
'$1 <link media="all" rel="stylesheet" type="text/css" href="style.css"/>$2$3</head>',
'$1 <meta http-equiv="X-UA-Compatible" content="IE=edge" />$2 <link media="all" rel="stylesheet" type="text/css" href="style.css"/>$2$3</head>',
'$1 class="docs"',
);

View File

@@ -0,0 +1,173 @@
<?php
namespace phpdotnet\phd;
/* $Id: EnhancedCHM.php 307979 2011-02-03 17:21:14Z rquadling $ */
class Package_PHP_EnhancedCHM extends Package_PHP_CHM
{
// Do not bother processing notes if we haven't got them this time.
protected $haveNotes = false;
public function __construct() {
parent::__construct();
$this->registerFormatName("PHP-EnhancedCHM");
}
public function update($event, $val = null) {
switch($event) {
case Render::INIT:
parent::update($event, $val);
// Get http://www.php.net/backend/notes/all.bz2 and save it.
v('Downloading usernotes.', VERBOSE_MESSAGES);
if (false === ($userNotesArchive = file_get_contents('http://www.php.net/backend/notes/all.bz2'))) {
v('Failed to download usernotes.', E_USER_ERROR);
break;
}
// Save the usernotes.
v('Saving usernotes.', VERBOSE_MESSAGES);
if (false === file_put_contents(Config::output_dir() . 'all.bz2', $userNotesArchive)) {
v('Failed to save usernotes.', E_USER_ERROR);
break;
}
// Decompress the usernotes.
v('Decompressing usernotes.', VERBOSE_MESSAGES);
if (false === strpos(exec('bzip2.exe -dfkv ' . Config::output_dir() . 'all.bz2 2>&1'), 'all.bz2: done')) {
v('Failed to decompress usernotes.' . PHP_EOL . implode(PHP_EOL, $output), E_USER_ERROR);
break;
}
// Extract the usernotes and store them by page and date.
v('Preparing usernotes.', VERBOSE_MESSAGES);
// Create usernotes directory.
$this->userNotesDir = Config::output_dir() . 'usernotes' . DIRECTORY_SEPARATOR;
if(!file_exists($this->userNotesDir) || is_file($this->userNotesDir)) {
mkdir($this->userNotesDir) or v("Can't create the usernotes directory", E_USER_ERROR);
}
// Remove any existing files.
foreach(glob($this->userNotesDir . '*' . DIRECTORY_SEPARATOR . '*') as $sectionFile) {
unlink($sectionFile);
}
// Decompress the 'all' file into single files - one file per sectionid.
$userNotesFile = fopen(Config::output_dir() . 'all', 'rt');
while($userNotesFile && !feof($userNotesFile) && false !== ($userNote = fgetcsv($userNotesFile, 0, '|'))) {
// Usernote index
// 0 = Note ID
// 1 = Section ID
// 2 = Rate
// 3 = Timestamp
// 4 = User
// 5 = Note
$sectionHash = md5($userNote[1]);
$sectionDir = $this->userNotesDir . $sectionHash[0];
if (!file_exists($sectionDir)) {
mkdir($sectionDir);
}
file_put_contents($sectionDir . DIRECTORY_SEPARATOR . $sectionHash, implode('|', $userNote) . PHP_EOL, FILE_APPEND);
}
fclose($userNotesFile);
$this->haveNotes = true;
v('Usernotes prepared.', VERBOSE_MESSAGES);
// Use classes rather than colors.
ini_set('highlight.comment', 'comment');
ini_set('highlight.default', 'default');
ini_set('highlight.keyword', 'keyword');
ini_set('highlight.string', 'string');
ini_set('highlight.html', 'html');
break;
default:
parent::update($event, $val);
}
}
public function footer($id) {
$footer = parent::footer($id);
// Find usernotes file.
$idHash = md5($id);
$userNotesFile = $this->userNotesDir . $idHash[0] . DIRECTORY_SEPARATOR . $idHash;
if (!file_exists($userNotesFile)) {
$notes = ' <div class="note">There are no user contributed notes for this page.</div>';
} else {
$notes = '';
foreach(file($userNotesFile) as $userNote) {
list($noteId, $noteSection, $noteRate, $noteTimestamp, $noteUser, $noteText) = explode('|', $userNote);
if ($noteUser) {
$noteUser = '<strong class="user">' . htmlspecialchars($noteUser) . '</strong>';
}
$noteDate = '<a href="#' . $noteId . '" class="date">' . date("d-M-Y h:i", $noteTimestamp) . '</a>';
$anchor = '<a name="' . $noteId . '""></a>';
$noteText = str_replace(
array(
'&nbsp;',
'<br />',
'<font color="', // for PHP 4
'<span style="color: ', // from PHP 5.0.0RC1
'</font>',
"\n ",
' ',
' '
),
array(
' ',
"<br />\n",
'<span class="',
'<span class="',
'</span>',
"\n&nbsp;",
'&nbsp; ',
'&nbsp; '
),
preg_replace(
'!((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$)!',
'<a href="\1" rel="nofollow" target="_blank">\1</a>\4',
highlight_string(trim(base64_decode($noteText)), true))
);
$notes .= <<< END_NOTE
{$anchor}
<div class="note">
{$noteUser}
{$noteDate}
<div class="text">
<div class="phpcode">
{$noteText}
</div>
</div>
</div>
<div class="foot"></div>
END_NOTE;
}
$notes = '<div id="allnotes">' . $notes . '</div>';
}
return <<< USER_NOTES
<div id="usernotes">
<div class="head">
<h3 class="title">User Contributed Notes</h3>
</div>
{$notes}
</div>
{$footer}
USER_NOTES;
}
}

View File

@@ -15,6 +15,7 @@ class Package_PHP_Factory extends Format_Factory {
'chm' => 'Package_PHP_CHM',
'tocfeed' => 'Package_PHP_TocFeed',
'epub' => 'Package_PHP_Epub',
'enhancedchm' => 'Package_PHP_EnhancedCHM',
);
public function __construct() {