Repository navigation
Extending DocParser‐PHP
Niccolò Vettorello edited this page Oct 1, 2025
·
1 revision
This guide explains how to extend DocParser-PHP to support a new markup language (e.g. Markdown, XML, LaTeX, etc...). By following these steps, you can integrate new parsers and validators while keeping the project’s modular architecture intact.
-
Define Parser and Validator Classes:
- Create a new folder
Languagewithinsrc/Model/Parser, e.g.src/Model/Parser/XML; - Create two new folders
ElementandValidatorinside of the folder, e.g.src/Model/Parser/XML/Elementandsrc/Model/Parser/XML/Validator; -
Elementfolder should contain the parsing logic and its classes must implementParserInterfaceinterface; -
Validatorfolder should contain the validation logic and its classes must extendAbstractValidatorclass.
- Create a new folder
-
Implement Validation Logic:
- Check the document structure (e.g., required headings, mandatory attributes).
- Generate errors and warnings via
ElementValidationResult.
-
Implement Parsing Logic:
- Parse the document content into a structured data format using the class
Node. - Ensure the parser output is compatible with the JSON view.
- Parse the document content into a structured data format using the class
- Within folder
config/Parsercreate a yaml file that contains the element roots from which the parser should start. These are classes contained in the folderElementpreviously defined. More than one root is allowed. The file must be name with the formatparser_language.yaml.
Example:
rootElements:
- Niccolo\DocparserPhp\Model\Parser\XML\Element\ParserClass- Within folder
config/Validatorcreate a yaml file that contains the validation classes whose validation is to be executed. These are classes contained in the folderValidatorpreviously defined. More than one class is allowed. The file must be name with the formatvalidator_language.yaml.
Example:
validators:
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\DoctypeValidator
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\HtmlValidator
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\HeadValidator
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\BodyValidator
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\TitleValidator
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\HeadingValidator
- Niccolo\DocparserPhp\Model\Parser\HTML\Validator\ParagraphValidator- Within the class
src/Model/Utils/Parser/Enum/InputTypeadd a new case for the new supported language, e.g.case XML = 'xml'; - In the same class add the extension for file written in that language (which may vary, e.g.
mdforMarkdown), e.g.self::XML->value => '.xml'.
- Validation Tests: Ensure the validator correctly detects errors and warnings.
- Parsing Tests: Check that the parser produces the correct data structure.
- Integration Tests: Verify the full flow: input → validation → parsing → output.
- Regression Tests: Ensure adding the new language does not break existing parsers.
-
Modify the User Interface:
- Add a new option in the input form to select the new language.
-
Handle User Selection:
- Ensure the selection is passed correctly to the controller to create the appropriate parser.
- Run PHPUnit Tests to verify all tests pass.
- Test the UI to ensure the new language works as expected.