├── .styleci.yml
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE.md
├── README.md
├── composer.json
└── src
├── Blockquote.php
├── Breaks.php
├── Code.php
├── Element.php
├── Heading.php
├── Image.php
├── Link.php
├── Markdown.php
├── Mutations.php
├── Table.php
├── TableRow.php
└── Todo.php
/.styleci.yml:
--------------------------------------------------------------------------------
1 | preset: laravel
2 |
3 | disabled:
4 | - single_class_element_per_statement
5 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to `phpdown` will be documented in this file
4 |
5 | ## 1.0.0 - 201X-XX-XX
6 |
7 | - initial release
8 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | Contributions are **welcome** and will be fully **credited**.
4 |
5 | Please read and understand the contribution guide before creating an issue or pull request.
6 |
7 | ## Etiquette
8 |
9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11 | extremely unfair for them to suffer abuse or anger for their hard work.
12 |
13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14 | world that developers are civilized and selfless people.
15 |
16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18 |
19 | ## Viability
20 |
21 | When requesting or submitting new features, first consider whether it might be useful to others. Open
22 | source projects are used by many developers, who may have entirely different needs to your own. Think about
23 | whether or not your feature is likely to be used by other users of the project.
24 |
25 | ## Procedure
26 |
27 | Before filing an issue:
28 |
29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30 | - Check to make sure your feature suggestion isn't already present within the project.
31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32 | - Check the pull requests tab to ensure that the feature isn't already in progress.
33 |
34 | Before submitting a pull request:
35 |
36 | - Check the codebase to ensure that your feature doesn't already exist.
37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38 |
39 | ## Requirements
40 |
41 | If the project maintainer has any additional requirements, you will find them listed here.
42 |
43 | - **[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](https://pear.php.net/package/PHP_CodeSniffer).
44 |
45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46 |
47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48 |
49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50 |
51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52 |
53 | - **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](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54 |
55 | **Happy coding**!
56 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Saleem Hadad
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Code-Driven PHP Markdown Representation
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | 
18 |
19 | # PHPDown
20 |
21 | ## Why? (optional)
22 |
23 | I know, it's a little bit funky to represent Markdown elements as PHP classes but let me show you the reason behind it.
24 |
25 | Currently, I'm still actively maintaining my package [Larecipe](https://larecipe.binarytorch.com.my) which its main purpose is writing software documentation in a very elegant and beautiful looking. However, during my daily work as software team leader, I'm required to write a lot of docs for our projects and APIs where now I feel I have to automate this process and save some time on other important things.
26 |
27 | Therefore, I've decided to develop a new open-source tool (will be available soon) for Larecipe where you can generate docs on the fly from your tests suites without warring on updating the docs every time you add new field or remove one and this idea requires to have Markdown generated output and yeah! you can guess the rest of the story 😆 but I really think this is helpful for other cases that's why I separated as a new package for PHP.
28 |
29 | ## Installation
30 |
31 | You can install the package via composer:
32 |
33 | ```bash
34 | composer require binarytorch/phpdown
35 | ```
36 |
37 | ## Usage & Supported Elements
38 |
39 | - [x] Heading
40 | - [x] Breaks
41 | - [x] Blockquote
42 | - [x] Link
43 | - [x] Image
44 | - [x] Todo List
45 | - [x] Mutations: [Strikethrough, Bold, Inline Code, Marked]
46 | - [x] Code
47 | - [x] Table
48 | - [x] Markdown Document
49 |
50 | See full [documentation](https://phpdown.binarytorch.com.my/docs)
51 |
52 | ## Contributing
53 |
54 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
55 |
56 | ### Security
57 |
58 | If you discover any security related issues, please email saleem@binarytorch.com.my instead of using the issue tracker.
59 |
60 | ## Credits
61 |
62 | - [Saleem Hadad](https://github.com/saleem-hadad)
63 | - [All Contributors](../../contributors)
64 |
65 | ## License
66 |
67 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
68 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "binarytorch/phpdown",
3 | "description": "Code-Driven PHP Markdown Representation",
4 | "keywords": [
5 | "binarytorch",
6 | "phpdown"
7 | ],
8 | "homepage": "https://github.com/binarytorch/phpdown",
9 | "license": "MIT",
10 | "type": "library",
11 | "authors": [
12 | {
13 | "name": "Saleem Hadad",
14 | "email": "saleem@binarytorch.com.my",
15 | "role": "Developer"
16 | }
17 | ],
18 | "require": {
19 | "php": "^7.1"
20 | },
21 | "require-dev": {
22 | "phpunit/phpunit": "^7.0"
23 | },
24 | "autoload": {
25 | "psr-4": {
26 | "BinaryTorch\\PhpDown\\": "src"
27 | }
28 | },
29 | "autoload-dev": {
30 | "psr-4": {
31 | "BinaryTorch\\PhpDown\\Tests\\": "tests"
32 | }
33 | },
34 | "scripts": {
35 | "test": "vendor/bin/phpunit",
36 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage"
37 | },
38 | "config": {
39 | "sort-packages": true
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Blockquote.php:
--------------------------------------------------------------------------------
1 | ";
13 | }
--------------------------------------------------------------------------------
/src/Breaks.php:
--------------------------------------------------------------------------------
1 | content = $content;
34 | $this->language = $language;
35 | }
36 |
37 | /**
38 | * @return mixed
39 | */
40 | public function setLanguage($language)
41 | {
42 | $this->language = $language;
43 |
44 | return $this;
45 | }
46 |
47 | /**
48 | * @return mixed
49 | */
50 | public function operand()
51 | {
52 | return "{$this->operand}{$this->language}\n";
53 | }
54 |
55 | /**
56 | * @return mixed
57 | */
58 | public function endOperand()
59 | {
60 | return "\n{$this->endOperand}";
61 | }
62 | }
--------------------------------------------------------------------------------
/src/Element.php:
--------------------------------------------------------------------------------
1 | content = $content;
39 | }
40 |
41 | /**
42 | * Create a new element.
43 | *
44 | * @return static
45 | */
46 | public static function make(...$arguments)
47 | {
48 | return new static(...$arguments);
49 | }
50 |
51 | /**
52 | * @return mixed
53 | */
54 | public function operand()
55 | {
56 | return $this->operand;
57 | }
58 |
59 | /**
60 | * @return mixed
61 | */
62 | public function content()
63 | {
64 | return $this->content;
65 | }
66 |
67 | /**
68 | * @return mixed
69 | */
70 | public function endOperand()
71 | {
72 | return $this->endOperand;
73 | }
74 |
75 | /**
76 | * @return bool
77 | */
78 | public function withNewLine()
79 | {
80 | $this->newLine = true;
81 |
82 | return $this;
83 | }
84 |
85 | /**
86 | * @return mixed
87 | */
88 | public function render()
89 | {
90 | $result = "{$this->operand()}";
91 |
92 | if($this->content() && $this->canRenderContent()) {
93 | // handle the indented elements.
94 | if($this->content() instanceof Element) {
95 | $result .= " {$this->content()->render()}";
96 | }else {
97 | $result .= " {$this->content()}";
98 | }
99 | }
100 |
101 | if($this->endOperand()) {
102 | $result .= " {$this->endOperand()}";
103 | }
104 |
105 | if($this->newLine) {
106 | $result .= "\n";
107 | }
108 |
109 | return $result;
110 | }
111 |
112 | /**
113 | * @return bool
114 | */
115 | protected function canRenderContent()
116 | {
117 | return true;
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/src/Heading.php:
--------------------------------------------------------------------------------
1 | operand = "#";
20 |
21 | return $this;
22 | }
23 |
24 | /**
25 | * @return mixed
26 | */
27 | public function is2()
28 | {
29 | $this->operand = "##";
30 |
31 | return $this;
32 | }
33 |
34 | /**
35 | * @return mixed
36 | */
37 | public function is3()
38 | {
39 | $this->operand = "###";
40 |
41 | return $this;
42 | }
43 |
44 | /**
45 | * @return mixed
46 | */
47 | public function is4()
48 | {
49 | $this->operand = "####";
50 |
51 | return $this;
52 | }
53 |
54 | /**
55 | * @return mixed
56 | */
57 | public function is5()
58 | {
59 | $this->operand = "#####";
60 |
61 | return $this;
62 | }
63 |
64 | /**
65 | * @return mixed
66 | */
67 | public function is6()
68 | {
69 | $this->operand = "######";
70 |
71 | return $this;
72 | }
73 | }
--------------------------------------------------------------------------------
/src/Image.php:
--------------------------------------------------------------------------------
1 | title = $title;
26 | $this->url = $url;
27 | }
28 |
29 | /**
30 | * @return mixed
31 | */
32 | public function render()
33 | {
34 | return "[{$this->title}]({$this->url})";
35 | }
36 | }
--------------------------------------------------------------------------------
/src/Markdown.php:
--------------------------------------------------------------------------------
1 | content as $element) {
15 | $content .= "\n{$element->render()}";
16 | }
17 |
18 | return $content;
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Mutations.php:
--------------------------------------------------------------------------------
1 | operand = "~~";
13 | $this->endOperand = "~~";
14 |
15 | return $this;
16 | }
17 |
18 | /**
19 | * @return this
20 | */
21 | public function bold()
22 | {
23 | $this->operand = "**";
24 | $this->endOperand = "**";
25 |
26 | return $this;
27 | }
28 |
29 | /**
30 | * @return this
31 | */
32 | public function italic()
33 | {
34 | $this->operand = "*";
35 | $this->endOperand = "*";
36 |
37 | return $this;
38 | }
39 |
40 | /**
41 | * @return this
42 | */
43 | public function inlineCode()
44 | {
45 | $this->operand = "`";
46 | $this->endOperand = "`";
47 |
48 | return $this;
49 | }
50 |
51 | /**
52 | * @return this
53 | */
54 | public function mark()
55 | {
56 | $this->operand = "";
57 | $this->endOperand = "";
58 |
59 | return $this;
60 | }
61 |
62 | /**
63 | * @return mixed
64 | */
65 | public function render()
66 | {
67 | $result = "{$this->operand()}";
68 |
69 | if($this->content() && $this->canRenderContent()) {
70 | // handle the indented elements.
71 | if($this->content() instanceof Element) {
72 | $result .= "{$this->content()->render()}";
73 | }else {
74 | $result .= "{$this->content()}";
75 | }
76 | }
77 |
78 | $result .= "{$this->endOperand()}";
79 |
80 | if($this->newLine) {
81 | $result .= "\n";
82 | }
83 |
84 | return $result;
85 | }
86 | }
--------------------------------------------------------------------------------
/src/Table.php:
--------------------------------------------------------------------------------
1 | header = $header;
25 | $this->rows = $rows;
26 | }
27 |
28 | /**
29 | * @param $alignment
30 | * @return string
31 | */
32 | protected function getHeaderAlignment($alignment)
33 | {
34 | if($alignment == 'center') {
35 | return ':-:';
36 | }
37 |
38 | if($alignment == 'right') {
39 | return '-:';
40 | }
41 |
42 | return ':-';
43 | }
44 |
45 | /**
46 | * @return string
47 | */
48 | public function generateHeader()
49 | {
50 | $headerRow = "| ";
51 | $alignmentRow = "| ";
52 |
53 | foreach($this->header as $columnName => $alignment) {
54 | $headerRow .= "{$columnName} | ";
55 | $alignmentRow .= "{$this->getHeaderAlignment($alignment)} | ";
56 | }
57 |
58 | return "{$headerRow}\n{$alignmentRow}";
59 | }
60 |
61 | /**
62 | * @return string
63 | */
64 | public function generateRows()
65 | {
66 | $rows = "";
67 |
68 | foreach($this->rows as $row) {
69 | $rows .= $row->withNewLine()->render();
70 | }
71 |
72 | return $rows;
73 | }
74 |
75 | /**
76 | * @return mixed
77 | */
78 | public function render()
79 | {
80 | // die(var_dump($this->generateRows()));
81 | return "{$this->generateHeader()}\n{$this->generateRows()}";
82 | }
83 | }
--------------------------------------------------------------------------------
/src/TableRow.php:
--------------------------------------------------------------------------------
1 | content as $row) {
22 | $content .= "{$row} | ";
23 | }
24 |
25 | return $content;
26 | }
27 | }
--------------------------------------------------------------------------------
/src/Todo.php:
--------------------------------------------------------------------------------
1 | isCompleted = true;
20 |
21 | return $this;
22 | }
23 |
24 | /**
25 | * @return mixed
26 | */
27 | public function operand()
28 | {
29 | return $this->isCompleted ? "- [x]" : "- [ ]";
30 | }
31 | }
--------------------------------------------------------------------------------