Skip to content

Commit

Permalink
Issue #7 - start writing logic to generate a .pot file from an .html …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
bobbingwide committed Nov 26, 2020
1 parent 3624b1e commit bb57ba7
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 16 deletions.
97 changes: 97 additions & 0 deletions class-potter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

class Potter {
public $pot_filename;
public $source_filename;
public $project;

function __construct() {
}

function set_pot_filename( $filename ) {
$this->pot_filename = $filename;
}

function set_project( $project ) {
$this->project = $project;
}

function write_strings( $strings ) {
$output = '';
foreach ( $strings as $string => $filename ) {
$output .= $this->write_fileline( $filename );
$output .= $this->write_string( $string );
$output .= $this->write_blank();
}
return $output;
}

function write_fileline( $filename ) {
$output = '#: ' . $filename;
$output .= PHP_EOL;

return $output;
}

function write_string( $string ) {
$output = 'msgid "' . $string . '"';
$output .= PHP_EOL;
$output .= 'msgstr ""';
return $output;
}

function write_blank() {
$output = PHP_EOL;
$output .= '';
$output .= PHP_EOL;
return $output;
}



function write_header() {
$output = [];
$output[] = '# Copyright (C) 2020';
$output[] = '# This file is distributed under the same licence as WordPress';
$output[] = $this->write_string('');
$output[] = "\"Project-Id-Version: {$this->project}\\n\"";
$output[] = "\"Report-Msgid-Bugs-To: http://wordpress.org/tag/{$this->project}\\n\"";
$output[] = "\"POT-Creation-Date: ". date('Y-m-d h:i:s') ."+00:00\\n\"";
$output[] = "\"MIME-Version: 1.0\\n\"";
$output[] = "\"Content-Type: text/plain; charset=UTF-8\\n\"";
$output[] = "\"Content-Transfer-Encoding: 8bit\\n\"";
$output[] = "\"PO-Revision-Date: 2020-MO-DA HO:MI+ZONE\\n\"";
$output[] = "\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"";
$output[] = "\"Language-Team: LANGUAGE <[email protected]>\\n\"";
$output[] = '';
$output = implode( PHP_EOL, $output);
$output .= PHP_EOL;
return $output;

}
}

/*
# Copyright (C) 2020 oik
# This file is distributed under the same license as the oik package.
msgid ""
msgstr ""
"Project-Id-Version: oik 4.1.0\n"
"Report-Msgid-Bugs-To: http://wordpress.org/tag/oik\n"
"POT-Creation-Date: 2020-09-04 13:46:15+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"PO-Revision-Date: 2020-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
#: admin/class-bw-list-table.php:153
msgid "List View"
msgstr ""
#: admin/class-bw-list-table.php:154
msgid "Excerpt View"
msgstr ""
*/
59 changes: 51 additions & 8 deletions class-stringer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Class Stringer
*
* Implements simple_html_dom parsing for WordPress template and template part .html files
* to find the translatable strings and make them available for translation
* to find the translatable strings and make them available for translation.
*
* The second phase is to replace the translatable strings with the translations
* and create new locale specific versions of the theme's files
* and create new locale specific versions of the theme's files.
*
*/

Expand All @@ -16,27 +16,46 @@ class Stringer {

private $notNeededTags = [];

public $strings = [];

public $source_filename = null;

function __construct() {
if ( ! function_exists( 'str_get_html' ) ) {
require_once 'simple_html_dom.php';
}
$this->setUpNotNeeded();
}

function get_strings( $block ) {
$html = str_get_html( $block['innerHTML'] );
// print_r( $html );
//$html->dump();
//foreach ( $html->root->children as $node ) {
//print_r( $node );
function set_source_filename( $filename ) {
$this->source_filename = $filename;
}

/**
* Gets translatable strings from the inner HTML for a block.
*
*/
function get_strings( $innerHTML ) {
$html = str_get_html( $innerHTML );
$this->recurse( $html->root );
return $this->strings;
}

/**
* Lists the HTML tags where we don't need to extract text.
*
* The HTML parser can duplicate text from inner tags
* such as ul, ol and others yet to be discovered.
*
*/
function setUpNotNeeded() {
$notNeededTags = [ 'ul', 'ol'];
$this->notNeededTags = array_flip( $notNeededTags );
}

/**
* Checks if we need the text for this tag.
*/
function isInnertextNeeded( $tag ) {
if ( isset( $this->notNeededTags[$tag ] ) ) {
return false;
Expand All @@ -45,6 +64,11 @@ function isInnertextNeeded( $tag ) {

}

/**
* Extracts translatable strings from the parsed HTML.
*
* @param $node
*/
function recurse( $node ) {
static $nest = 0;
$nest++;
Expand All @@ -61,6 +85,7 @@ function recurse( $node ) {
$text=$node->innertext();
$text=trim( $text );
echo $text;
$this->add_string( $text );
}
}
echo implode( ' ', $node->getAllAttributes() );
Expand Down Expand Up @@ -91,4 +116,22 @@ function recurse( $node ) {
echo PHP_EOL;
}

/**
* Add a translatable string.
* @param $text
*/
function add_string( $text ) {
if ( !isset( $this->strings[ $text ] ) ) {
$this->strings[$text] = $this->source_filename;
}
}

/**
* Dumps the strings to a .pot file
*
*/
function get_all_strings() {
return $this->strings;
}

}
29 changes: 22 additions & 7 deletions html2pot.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
$filename = 'test.html';

$html = file_get_contents( $filename);
print_r( $html );
//print_r( $html );

if ( 0 === strlen( $html) ) {
echo "Invalid file: " . $filename;
exit();

gob();
}

require_once 'class-stringer.php';
require_once 'class-potter.php';

/**
* Use Gutenberg to parse the content into individual blocks.
Expand All @@ -48,15 +48,30 @@
$parser = new WP_Block_Parser();
$blocks = $parser->parse( $html );

print_r( $blocks );
//print_r( $blocks );

$stringer = new Stringer();
$count = 0;
$stringer->set_source_filename( $filename );
foreach ( $blocks as $block) {
$count++;
echo PHP_EOL;
echo "Block: " . $count;
echo PHP_EOL;
$stringer->get_strings( $block );
//print_r( $block );

print_r( $block );
if ( !empty( $block['innerHTML'] ) ) {
$strings = $stringer->get_strings( $block['innerHTML'] );
//print_r( $block );
//$strings =
//$potter->write_strings( $strings );
}
}

$strings = $stringer->get_all_strings();
$potter = new Potter();
$potter->set_pot_filename( 'fizzie.pot');
$potter->set_project( 'fizzie');
$output = $potter->write_header();
$output .= $potter->write_strings( $strings );
echo $output;

22 changes: 21 additions & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,24 @@ <h2>Heading</h2>
<li>List item 1</li>
<li>List item 2</li>
<li>List <b>item</b> 3</li>
</ul>
</ul>

<div class="WP_DEBUG">page-13.html 12:22</div>

<!-- wp:template-part {"slug":"header","theme":"fizzie"} /-->

<!-- wp:template-part {"slug":"breadcrumbs","theme":"fizzie", "className": "breadcrumbs"} /-->

<!-- wp:template-part {"slug":"post-content","theme":"fizzie", "className": "post-content"} /-->

<!-- wp:template-part {"slug":"metadates", "theme":"fizzie", "className": "metadates" } /-->

<!-- wp:template-part {"slug":"information","theme":"fizzie", "className": "information"} /-->

<!-- wp:template-part {"slug":"search","theme":"fizzie"} /-->

<!-- wp:template-part {"slug":"page-footer","theme":"fizzie"} /-->

<!-- wp:template-part {"slug":"footer","theme":"fizzie"} /-->

<!-- wp:template-part {"slug":"footer-menu","theme":"fizzie", "className":"footer-menu"} /-->

0 comments on commit bb57ba7

Please sign in to comment.