Skip to content

Commit

Permalink
fix: review the document title extraction & fix tests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-picas committed Feb 12, 2024
1 parent a704dbc commit b9b3926
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .phpmd.baseline.xml
Expand Up @@ -100,10 +100,12 @@
<violation rule="PHPMD\Rule\UnusedLocalVariable" file="src/MarkdownExtended/OutputFormat/Man.php"/>
<violation rule="PHPMD\Rule\CleanCode\StaticAccess" file="src/MarkdownExtended/OutputFormatBag.php"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="src/MarkdownExtended/OutputFormatBag.php"/>
<violation rule="PHPMD\Rule\Design\WeightedMethodCount" file="src/MarkdownExtended/Parser.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/MarkdownExtended/Parser.php"/>
<violation rule="PHPMD\Rule\CleanCode\StaticAccess" file="src/MarkdownExtended/Parser.php"/>
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/MarkdownExtended/Parser.php" method="setOptions"/>
<violation rule="PHPMD\Rule\CleanCode\BooleanArgumentFlag" file="src/MarkdownExtended/Parser.php"/>
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/MarkdownExtended/Parser.php" method="transform"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="src/MarkdownExtended/Parser.php"/>
<violation rule="PHPMD\Rule\UnusedLocalVariable" file="src/MarkdownExtended/Parser.php"/>
<violation rule="PHPMD\Rule\UnusedPrivateMethod" file="src/MarkdownExtended/Parser.php" method="_hardDebugContent"/>
Expand All @@ -113,8 +115,8 @@
<violation rule="PHPMD\Rule\Naming\ShortVariable" file="src/MarkdownExtended/Util/DomIdRegistry.php"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="src/MarkdownExtended/Util/Helper.php"/>
<violation rule="PHPMD\Rule\CleanCode\StaticAccess" file="src/MarkdownExtended/Util/Helper.php"/>
<violation rule="PHPMD\Rule\CleanCode\BooleanArgumentFlag" file="src/MarkdownExtended/Util/Helper.php"/>
<violation rule="PHPMD\Rule\Naming\ShortVariable" file="src/MarkdownExtended/Util/Helper.php"/>
<violation rule="PHPMD\Rule\CleanCode\BooleanArgumentFlag" file="src/MarkdownExtended/Util/Helper.php"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="src/MarkdownExtended/Util/Registry.php"/>
<violation rule="PHPMD\Rule\CleanCode\StaticAccess" file="src/markdown.php"/>
</phpmd-baseline>
2 changes: 1 addition & 1 deletion demo/.htaccess
Expand Up @@ -35,6 +35,6 @@ AddType text/html .md
# Treat '.md' files by the Markdown handler
# CAUTION - this requires to know exactly where the CGI is ...
AddHandler MarkDown .md
Action MarkDown /cgi-scripts/mde_apacheHandler.sh virtual
Action MarkDown /demo/cgi-scripts/mde_apacheHandler.sh virtual

# Endfile
22 changes: 11 additions & 11 deletions doc/Development-HOWTO.md
Expand Up @@ -7,7 +7,7 @@ This documentation file will introduce you the "dev-cycle" of PHP MarkdownExtend
First of all, you may do the following two things:

- read the *How to contribute* section below to learn about forking, working and pulling,
- from your fork of the repository, switch to the `dev` branch: this is where the dev things are done.
- from your fork of the repository, switch to the `develop` branch: this is where the dev things are done.


### How to contribute ?
Expand Down Expand Up @@ -36,11 +36,11 @@ comment the request with your vision of the thing or your experience.
### Full installation of a fork

To prepare a development version of PHP MarkdownExtended, clone your fork of the repository and
put it on the "dev" branch:
put it on the "develop" branch:

git clone http://github.com/<your-username>/markdown-extended.git
cd markdown-extended
git checkout dev
git checkout develop

Then you can create your own branch with the name of your feature:

Expand All @@ -64,18 +64,18 @@ and pulling new commits:
git remote add upstream http://github.com/e-picas/markdown-extended.git

// get last original remote commits
git checkout dev
git pull upstream dev
git checkout develop
git pull upstream develop


### Development life-cycle

As said above, all development MUST be done on the `dev` branch of the repository. Doing so we
As said above, all development MUST be done on the `develop` branch of the repository. Doing so we
can commit our development features to let users using a clone test and improve them.

When the work gets a stable stage, it seems to be time to build and publish a new release. This
is done by creating a tag named like `vX.Y.Z[-status]` from the "master" branch after having
merged the "dev" one in. Please see the [Semantic Versioning](http://semver.org/) work by
merged the "develop" one in. Please see the [Semantic Versioning](http://semver.org/) work by
Tom Preston-Werner for more info about the release version name construction rules.


Expand Down Expand Up @@ -177,10 +177,10 @@ Note that the package is integrated in [Code Climate](https://codeclimate.com/gi

To make a new release of the package, you must follow these steps:

1. merge the "dev" branch into "master"
1. merge the "develop" branch into "master"

git checkout master
git merge --no-ff --no-commit dev
git merge --no-ff --no-commit develop

2. fix code standards errors:

Expand Down Expand Up @@ -212,9 +212,9 @@ To make a new release of the package, you must follow these steps:
rm -f bin/markdown-extended.phar && mv markdown-extended.phar bin/
git commit -a -m "new X.Y.Z-STATE phar"

9. merge "master" into "dev":
9. merge "master" into "develop":

git checkout dev
git checkout develop
git merge --no-ff master

Finally, don't forget to push all changes to `origin` and to make a release page
Expand Down
10 changes: 10 additions & 0 deletions src/MarkdownExtended/Parser.php
Expand Up @@ -140,6 +140,16 @@ public function transform($content, $name = null, $primary = true)
// actually parse content
$content = $this->parseContent($content);

// guess the title if it is NOT empty
// @TODO - Try to make it better extracting directly from the MD source
// something strange is here: \MarkdownExtended\Grammar\Filter\Header::_setContentTitle()
if (strtolower($this->getKernel()->getConfig('output_format')) == 'html') {
$titles = Helper::getTextBetweenTags($content->getBody(), 'h1');
if (isset($titles[0])) {
$content->setTitle($titles[0]);
}
}

// force template if needed
$tpl = $this->getKernel()->getConfig('template');
if (!is_null($tpl) && $tpl === 'auto') {
Expand Down
19 changes: 19 additions & 0 deletions src/MarkdownExtended/Util/Helper.php
Expand Up @@ -156,6 +156,25 @@ public static function isSingleLine($str = '')
return (bool) (false === strpos($str, PHP_EOL));
}

/**
* Extract all contents of a specific HTML tag from a content
*
* @param string $string The HTML string to search in
* @param string $tagname The tagname to extract
*
* @return array
*/
public static function getTextBetweenTags($string, $tagname)
{
$d = new \DOMDocument();
$d->loadHTML($string);
$return = [];
foreach($d->getElementsByTagName($tagname) as $item) {
$return[] = $item->textContent;
}
return $return;
}

// --------------
// Regular expressions
// --------------
Expand Down

0 comments on commit b9b3926

Please sign in to comment.