From f910a117a4f6b12d87de2737cbe252a34ad37748 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Mon, 11 Mar 2019 10:08:21 -0400 Subject: [PATCH] Initial commit --- .editorconfig | 15 +++++ .gitattributes | 11 ++++ .gitignore | 4 ++ .scrutinizer.yml | 39 ++++++++++++ .styleci.yml | 9 +++ .travis.yml | 26 ++++++++ CHANGELOG.md | 11 ++++ CONDUCT.md | 22 +++++++ CONTRIBUTING.md | 51 +++++++++++++++ LICENSE.md | 32 ++++++++++ README.md | 79 ++++++++++++++++++++++++ composer.json | 41 ++++++++++++ phpunit.xml.dist | 29 +++++++++ src/ChildRenderer.php | 47 ++++++++++++++ src/InlinesOnlyExtension.php | 96 +++++++++++++++++++++++++++++ tests/InlinesOnlyFunctionalTest.php | 75 ++++++++++++++++++++++ tests/inlines.html | 8 +++ tests/inlines.md | 9 +++ 18 files changed, 604 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .scrutinizer.yml create mode 100644 .styleci.yml create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/ChildRenderer.php create mode 100644 src/InlinesOnlyExtension.php create mode 100644 tests/InlinesOnlyFunctionalTest.php create mode 100644 tests/inlines.html create mode 100644 tests/inlines.md diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cd8eb86 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..43590dd --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.gitattributes export-ignore +/.gitignore export-ignore +/.scrutinizer.yml export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/scrutinizer.yml export-ignore +/tests export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7aa851f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea/ +/build/ +/composer.lock +/vendor/ diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..a2baf35 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,39 @@ +filter: + excluded_paths: [tests/*] + +checks: + php: + code_rating: true + remove_extra_empty_lines: true + remove_php_closing_tag: true + remove_trailing_whitespace: true + fix_use_statements: + remove_unused: true + preserve_multiple: false + preserve_blanklines: true + order_alphabetically: true + fix_php_opening_tag: true + fix_linefeed: true + fix_line_ending: true + fix_identation_4spaces: true + fix_doc_comments: true + +tools: + external_code_coverage: + timeout: 600 + runs: 3 + php_analyzer: true + php_code_coverage: false + php_code_sniffer: + config: + standard: PSR2 + filter: + paths: ['src'] + php_cpd: + enabled: true + excluded_dirs: [vendor, tests] + php_loc: + enabled: true + excluded_dirs: [vendor, tests] + php_pdepend: true + php_sim: true diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..bc9a4c8 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1,9 @@ +preset: recommended + +enabled: + - concat_with_spaces + - strict + +disabled: + - concat_without_spaces + - phpdoc_summary diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..accc57e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +language: php + +php: + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - 7.3 + +env: + matrix: + - COMPOSER_FLAGS="" + - COMPOSER_FLAGS="--prefer-stable --prefer-lowest" + +sudo: false + +before_script: + - travis_retry composer self-update + - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source + +script: + - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover + +after_script: + - wget https://github.com/scrutinizer-ci/ocular/releases/download/1.5.2/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d3dc3f1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog + +All notable changes will be documented in this file. + +## [Unreleased][unreleased] + +## 0.1.0 - 2019-03-11 + +Initial release! + +[unreleased]: https://github.com/thephpleague/commonmark-ext-inlines-only/compare/0.1.0...HEAD diff --git a/CONDUCT.md b/CONDUCT.md new file mode 100644 index 0000000..6ff94ca --- /dev/null +++ b/CONDUCT.md @@ -0,0 +1,22 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery +* Personal attacks +* Trolling or insulting/derogatory comments +* Public or private harassment +* Publishing other's private information, such as physical or electronic addresses, without explicit permission +* Other unethical or unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. + +This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community in a direct capacity. Personal views, beliefs and values of individuals do not necessarily reflect those of the organisation or affiliated individuals and organisations. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4d9c7bd --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,51 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/thephpleague/commonmark-ext-inlines-only). + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **Create feature branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +- **Docblocks** - All files should start with the following docblock: + +~~~ +/* + * This file is part of the league/commonmark-ext-inlines-only package. + * + * (c) Colin O'Dell + * + * Authored by Your Name + * + * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js) + * - (c) John MacFarlane + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +~~~ + +The "Authored by" line is optional. + + +## Running Tests + +``` bash +$ composer test +``` + + +**Happy coding**! diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..ce4d207 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,32 @@ +Copyright (c) 2014-2019, Colin O'Dell + +Portions of this code based on commonmark.js: Copyright (c) 2014-2016, John MacFarlane + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Colin O'Dell nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fc55cbd --- /dev/null +++ b/README.md @@ -0,0 +1,79 @@ +# Inline-only extension for `league/commonmark` + +[![Latest Version on Packagist][ico-version]][link-packagist] +[![Software License][ico-license]](LICENSE.md) +[![Build Status][ico-travis]][link-travis] +[![Coverage Status][ico-scrutinizer]][link-scrutinizer] +[![Quality Score][ico-code-quality]][link-code-quality] +[![Total Downloads][ico-downloads]][link-downloads] + +This extension configures the [`league/commonmark` Markdown parser for PHP](https://github.com/thephpleague/commonmark) to only render inline elements - no paragraph tags, headers, code blocks, etc. + +## Install + +Via Composer + +``` bash +$ composer require league/commonmark-ext-inlines-only +``` + +## Usage + +Although you normally add extra extensions to the default core one, we're not going to do that here, because this is essentially a slimmed-down version of the core extension: + +``` php +use League\CommonMark\CommonMarkConverter; +use League\CommonMark\Environment; +use League\CommonMark\Ext\InlinesOnly\InlinesOnlyExtension; + +// Create a new, empty environment +$environment = new Environment(); + +// Add this extension +$environment->addExtension(new InlinesOnlyExtension()); + +// Instantiate the converter engine and start converting some Markdown! +$converter = new CommonMarkConverter($config, $environment); +echo $converter->convertToHtml('**Hello World!**'); +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. + +## Testing + +``` bash +$ composer test +``` + +## Security + +If you discover any security related issues, please email colinodell@gmail.com instead of using the issue tracker. + +## Credits + +- [Colin O'Dell][link-author] +- [John MacFarlane][link-jgm] +- [All Contributors][link-contributors] + +## License + +This library is licensed under the BSD-3 license. See the [License File](LICENSE.md) for more information. + +[ico-version]: https://img.shields.io/packagist/v/league/commonmark-ext-inlines-only.svg?style=flat-square +[ico-license]: http://img.shields.io/badge/License-BSD--3-brightgreen.svg?style=flat-square +[ico-travis]: https://img.shields.io/travis/thephpleague/commonmark-ext-inlines-only/master.svg?style=flat-square +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/thephpleague/commonmark-ext-inlines-only.svg?style=flat-square +[ico-code-quality]: https://img.shields.io/scrutinizer/g/thephpleague/commonmark-ext-inlines-only.svg?style=flat-square +[ico-downloads]: https://img.shields.io/packagist/dt/league/commonmark-ext-inlines-only.svg?style=flat-square + +[link-packagist]: https://packagist.org/packages/league/commonmark-ext-inlines-only +[link-travis]: https://travis-ci.org/thephpleague/commonmark-ext-inlines-only +[link-scrutinizer]: https://scrutinizer-ci.com/g/thephpleague/commonmark-ext-inlines-only/code-structure +[link-code-quality]: https://scrutinizer-ci.com/g/thephpleague/commonmark-ext-inlines-only +[link-downloads]: https://packagist.org/packages/league/commonmark-ext-inlines-only +[link-author]: https://github.com/colinodell +[link-contributors]: ../../contributors +[link-league-commonmark]: https://github.com/thephpleague/commonmark +[link-jgm]: https://github.com/jgm diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0e3045f --- /dev/null +++ b/composer.json @@ -0,0 +1,41 @@ +{ + "name": "league/commonmark-ext-inlines-only", + "type": "commonmark-extension", + "description": "Extension for league/commonmark which only renders inline text", + "keywords": ["markdown", "commonmark", "extension", "inline"], + "homepage": "https://github.com/thephpleague/commonmark-ext-inlines-only", + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Colin O'Dell", + "email": "colinodell@gmail.com", + "homepage": "https://www.colinodell.com", + "role": "Lead Developer" + } + ], + "require": { + "php" : ">=5.6", + "league/commonmark": "^0.18.1" + }, + "require-dev": { + "phpunit/phpunit": "^5.7" + }, + "autoload": { + "psr-4": { + "League\\CommonMark\\Ext\\InlinesOnly\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "league\\CommonMark\\Ext\\InlinesOnly\\Test\\": "tests" + } + }, + "scripts": { + "test": "phpunit" + }, + "extra": { + "branch-alias": { + "dev-master": "0.2-dev" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..d5f268e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,29 @@ + + + + + tests + + + + + src/ + + + + + + + + + + diff --git a/src/ChildRenderer.php b/src/ChildRenderer.php new file mode 100644 index 0000000..621b4eb --- /dev/null +++ b/src/ChildRenderer.php @@ -0,0 +1,47 @@ + + * + * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js) + * - (c) John MacFarlane + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace League\CommonMark\Ext\InlinesOnly; + +use League\CommonMark\Block\Element\AbstractBlock; +use League\CommonMark\Block\Element\Document; +use League\CommonMark\Block\Element\InlineContainerInterface; +use League\CommonMark\Block\Renderer\BlockRendererInterface; +use League\CommonMark\ElementRendererInterface; + +/** + * Simply renders child elements as-is, adding newlines as needed. + */ +final class ChildRenderer implements BlockRendererInterface +{ + /** + * {@inheritdoc} + */ + public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) + { + $out = ''; + + if ($block instanceof InlineContainerInterface) { + $out .= $htmlRenderer->renderInlines($block->children()); + } else { + $out .= $htmlRenderer->renderBlocks($block->children()); + } + + if (!($block instanceof Document)) { + $out .= "\n"; + } + + return $out; + } +} diff --git a/src/InlinesOnlyExtension.php b/src/InlinesOnlyExtension.php new file mode 100644 index 0000000..266593e --- /dev/null +++ b/src/InlinesOnlyExtension.php @@ -0,0 +1,96 @@ + + * + * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js) + * - (c) John MacFarlane + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace League\CommonMark\Ext\InlinesOnly; + +use League\CommonMark\Block\Element\Document; +use League\CommonMark\Block\Element\Paragraph; +use League\CommonMark\Block\Parser as BlockParser; +use League\CommonMark\Extension\Extension; +use League\CommonMark\Inline\Element as InlineElement; +use League\CommonMark\Inline\Parser as InlineParser; +use League\CommonMark\Inline\Processor as InlineProcessor; +use League\CommonMark\Inline\Renderer as InlineRenderer; + +final class InlinesOnlyExtension extends Extension +{ + /** + * {@inheritdoc} + */ + public function getBlockParsers() + { + return [ + new BlockParser\LazyParagraphParser(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getInlineParsers() + { + return [ + new InlineParser\NewlineParser(), + new InlineParser\BacktickParser(), + new InlineParser\EscapableParser(), + new InlineParser\EntityParser(), + new InlineParser\EmphasisParser(), + new InlineParser\AutolinkParser(), + new InlineParser\HtmlInlineParser(), + new InlineParser\CloseBracketParser(), + new InlineParser\OpenBracketParser(), + new InlineParser\BangParser(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getInlineProcessors() + { + return [ + new InlineProcessor\EmphasisProcessor(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getBlockRenderers() + { + $renderer = new ChildRenderer(); + + return [ + Document::class => $renderer, + Paragraph::class => $renderer, + ]; + } + + /** + * {@inheritdoc} + */ + public function getInlineRenderers() + { + return [ + InlineElement\Code::class => new InlineRenderer\CodeRenderer(), + InlineElement\Emphasis::class => new InlineRenderer\EmphasisRenderer(), + InlineElement\HtmlInline::class => new InlineRenderer\HtmlInlineRenderer(), + InlineElement\Image::class => new InlineRenderer\ImageRenderer(), + InlineElement\Link::class => new InlineRenderer\LinkRenderer(), + InlineElement\Newline::class => new InlineRenderer\NewlineRenderer(), + InlineElement\Strong::class => new InlineRenderer\StrongRenderer(), + InlineElement\Text::class => new InlineRenderer\TextRenderer(), + ]; + } +} diff --git a/tests/InlinesOnlyFunctionalTest.php b/tests/InlinesOnlyFunctionalTest.php new file mode 100644 index 0000000..fe0fb41 --- /dev/null +++ b/tests/InlinesOnlyFunctionalTest.php @@ -0,0 +1,75 @@ + + * + * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js) + * - (c) John MacFarlane + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace League\CommonMark\Ext\InlinesOnly\Tests; + +use League\CommonMark\CommonMarkConverter; +use League\CommonMark\Converter; +use League\CommonMark\DocParser; +use League\CommonMark\Environment; +use League\CommonMark\Ext\InlinesOnly\InlinesOnlyExtension; +use League\CommonMark\HtmlRenderer; +use PHPUnit\Framework\TestCase; + +/** + * Tests the extension against sample files + */ +class InlinesOnlyFunctionalTest extends TestCase +{ + /** + * @var CommonMarkConverter + */ + protected $converter; + + protected function setUp() + { + $environment = new Environment(); + $environment->addExtension(new InlinesOnlyExtension()); + $this->converter = new Converter( + new DocParser($environment), + new HtmlRenderer($environment) + ); + } + + /** + * @param string $markdown Markdown to parse + * @param string $html Expected result + * + * @dataProvider dataProvider + */ + public function testExample($markdown, $html) + { + $actualResult = $this->converter->convertToHtml($markdown); + + $failureMessage = 'Unexpected result'; + $failureMessage .= "\n=== markdown ===============\n" . $markdown; + $failureMessage .= "\n=== expected ===============\n" . $html; + $failureMessage .= "\n=== got ====================\n" . $actualResult; + + $this->assertEquals($html, $actualResult, $failureMessage); + } + + /** + * @return array + */ + public function dataProvider() + { + $markdown = file_get_contents(__DIR__ . '/inlines.md'); + $html = file_get_contents(__DIR__ . '/inlines.html'); + + return [ + [$markdown, $html], + ]; + } +} diff --git a/tests/inlines.html b/tests/inlines.html new file mode 100644 index 0000000..f0c799b --- /dev/null +++ b/tests/inlines.html @@ -0,0 +1,8 @@ +Bold text normal text +Normal text and italics text too. + +Documentation is at https://commonmark.thephpleague.com. + +# This header will NOT be rendered in an h1 tag + +Two line breaks above diff --git a/tests/inlines.md b/tests/inlines.md new file mode 100644 index 0000000..b5b883c --- /dev/null +++ b/tests/inlines.md @@ -0,0 +1,9 @@ +**Bold text** normal text +Normal text and _italics text_ too. + +Documentation is at . + +# This header will NOT be rendered in an h1 tag + + +Two line breaks above