quantorconsulting / mkdocs_build_plantuml

MkDocs plugin to help generate your plantuml images locally or remotely as files (NOT inline)
MIT License
57 stars 15 forks source link

Error parsing PUML file with 2 consecutive !include statements #23

Closed derekmcn1 closed 2 years ago

derekmcn1 commented 2 years ago

Using mkdocs-build-plantuml-plugin 1.7.3, if there are 2 !include statements in a PUML file, without an extra line between them, like so:

!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4.puml
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml

an error is returned during processing

Converting /Users/foo/repo/docs/diagrams/src/blah.puml
Wrong response status for data-sci-env-context.puml: 400

After debugging this, I narrowed it down to the _readFileRec function in plantuml.py.

195     # Reads the file recursively
196     def _readFileRec(self, lines, temp_file, diagram, directory, dark_mode):
197
198         for line in lines:
199             line = line.strip()
200             if line.startswith("!include"):
201                 temp_file = self._readInclLine(
202                     diagram, line, temp_file, directory, dark_mode
203                 )
204             else:
205                 temp_file += line
206                 if "\n" not in line:
207                     temp_file += "\n"
208
209         return temp_file

If we are being called recursively because of an !include in a base PUML file, (i.e. line 201), and the included file does not end in a newline, then the next line read from the base file will be added to the end of the last line from the included file. This causes a syntax error when parsed by the PlantUML server..

A workaround is to put a blank line in between the 2 !include statements, thus resulting in a newline between the end of the included file and the next line in the base file.

Will provide a fix via a PR.

christo-ph commented 2 years ago

Released under 1.7.4

Thank you