Skip to content

Commit

Permalink
Adding the MetatagShell for translators
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 8, 2011
1 parent 0090f54 commit b43c12d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ Translations
Contributing translations requires that you make a new directory using the two letter name for your language. As content is translated, directories mirroring the english content should be created with localized content.


Generating Meta tags
--------------------

If you are providing translations and want to automatically generate meta tags for the resulting html files, a MetatagShell is provided in
the `scripts` folder of this repo. In order to use it, copy it into any CakePHP 2.0 empty application inside `app/Console/Command`, execute
`Console/cake metatag` and follow the instructions.

The script will process all the files under one of the translation folders and generate the possible description terms using an external API,
it is a good idea to go over the generated terms and clean-up whatever noise it might have generated.
68 changes: 68 additions & 0 deletions scripts/MetatagShell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

App::uses('HttpSocket', 'Network/Http');

/**
* Reads all the documentation files in a folder, process them and create meta tags for each one
* using the Yahoo terms extraction API
*
* Put this file in your app/Console/Command/ folder of any CakePHP 2.0 application and execute it as
* `app/Console/cake metatag`
*
*/
class MetatagShell extends Shell {

/**
* Reads all the files in a directory and process them to extract the description terms
*
* @return void
*/
public function main() {
$url = 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction';
$dir = $this->in('Please input the full path to the documentation folder (including the language directory)');
if (!is_dir($dir) && !is_file($dir . DS . 'index.rst')) {
throw new Exception('Invalid directory, please input the full path to one of the language directories for the docs repo');
}
$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)), '/\.rst$/');

foreach ($files as $item) {
if ($item->isDir()) {
continue;
}
$request = new HttpSocket;
$content = file($item->getRealPath());
$data = array(
'appid' => 'rrLaMQjV34HtIOsgPxf597DEP9KFoUzWybkmb4USJMPA89aCMWjjPFlnF3lD5ys-',
'query' => 'cakephp ' . $content[0],
'output' => 'json',
'context' => file_get_contents($item->getRealPath())
);
$result = $request->post('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction', $data);
$keywords = json_decode($result->body);
$keywords = $keywords->ResultSet->Result;
$meta = $this->generateMeta($keywords, str_replace("\n", '', $content[0]));

$fh = fopen($item->getRealPath(), 'a');
fwrite($fh, "\n\n" . $meta);
fclose($fh);
$this->out('<success>Processed</success> ' . $item->getRealPath());
}
}

/**
* Auxiliary function to generate the meta syntax in rst files
*
* @param array $keywords
* @param string $title
* @return string
*/
private function generateMeta($keywords, $title) {
$keywords = implode(',', $keywords);
$meta = <<<eom
.. meta::
:title lang=en: $title
:keywords lang=en: $keywords
eom;
return $meta;
}
}

0 comments on commit b43c12d

Please sign in to comment.