Skip to content
Snippets Groups Projects
Commit 6401024d authored by Andreas Kraft's avatar Andreas Kraft
Browse files

Merged additional changes from original GitHub repo

parent 78951d3f
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ python3 -m pip install -r requirements.txt ...@@ -13,7 +13,7 @@ python3 -m pip install -r requirements.txt
## Usage ## Usage
- Create a directory with the Word document in it. The Word document **must** be in *docx* format. This can be achieved by opening the document with *Word* and save it in *docx* format to another file. - Create a directory with the Word document in it. The Word document **must** be in *docx* format. This can be achieved by opening the document with *Word* and save it in *docx* format to another file.
- Create a configuration file with the same base name as the Word document + *.ini* extension. This file may contain different configurations as the standard *config.ini* file provided. - Create a configuration file with the same base name as the Word document + *.ini* extension. This file may contain different configurations as the standard *config.ini* file provided.
- Alternatively, a file named *config.ini* will apply to all files in that directory. - Alternatively, a file named *config.ini* will apply to all files in that directory.
- It is only necessary to add the settings that are different from the *config.ini* file in the project's root directory. That file will always act as a fallback. - It is only necessary to add the settings that are different from the *config.ini* file in the project's root directory. That file will always act as a fallback.
- Run the converter as follows: - Run the converter as follows:
``` ```
...@@ -26,6 +26,21 @@ python3 spec2md.py <path-to-word-document> ...@@ -26,6 +26,21 @@ python3 spec2md.py <path-to-word-document>
Is *LibreOffice* already running? If yes, then close it. Is *LibreOffice* already running? If yes, then close it.
### Does the converter generate links to tables?
If the table caption starts with "Table " and has a ":" after the table number then an HTML anchor with that number
is appended to the converted table caption. The anchor name is "table_\<table number>. This anchor can be used to link to the table, for example:
```markdown
[foo](#table_7.2.4-1)
or
<a href="#table_7.2.4-1">bar</a>
```
### Are linebreaks, paragraphs, and lists supported in table cells? ### Are linebreaks, paragraphs, and lists supported in table cells?
Unfortunately, markdown doesn't support multiple paragraphs in table cells. A table cell must be a single line. However, one can add a html `<br />` linebreak to break between lines: Unfortunately, markdown doesn't support multiple paragraphs in table cells. A table cell must be a single line. However, one can add a html `<br />` linebreak to break between lines:
......
...@@ -12,9 +12,11 @@ markdown-it-py==3.0.0 ...@@ -12,9 +12,11 @@ markdown-it-py==3.0.0
# via rich # via rich
mdurl==0.1.2 mdurl==0.1.2
# via markdown-it-py # via markdown-it-py
pygments==2.15.1 pygments==2.17.2
# via rich # via rich
python-docx==0.8.11 python-docx==1.1.0
# via oneM2M-spec-2-MD-converter (setup.py) # via oneM2M-spec-2-MD-converter (setup.py)
rich==13.4.2 rich==13.7.0
# via oneM2M-spec-2-MD-converter (setup.py) # via oneM2M-spec-2-MD-converter (setup.py)
typing-extensions==4.8.0
# via python-docx
...@@ -89,7 +89,7 @@ class SectionNumbers(object): ...@@ -89,7 +89,7 @@ class SectionNumbers(object):
self.annex:int = 64 self.annex:int = 64
def nextSectionNumber(self, level:int, isAnnex:bool = False) -> str: def nextSectionNumber(self, level:int, isAnnex:Optional[bool] = False) -> str:
if isAnnex: if isAnnex:
self.levels[0] = self.annex self.levels[0] = self.annex
else: else:
...@@ -190,7 +190,7 @@ class DocumentConfiguration(object): ...@@ -190,7 +190,7 @@ class DocumentConfiguration(object):
self.characters = { int(c, 16) : binascii.unhexlify(config.get('characters', c)).decode('utf-8') # type: ignore [attr-defined] self.characters = { int(c, 16) : binascii.unhexlify(config.get('characters', c)).decode('utf-8') # type: ignore [attr-defined]
for c in config['characters'] } for c in config['characters'] }
# Media # Media & Converter
self.emfConverterPng = config.get('media', 'emfConverterPng', fallback = None) self.emfConverterPng = config.get('media', 'emfConverterPng', fallback = None)
self.emfConverterSvg = config.get('media', 'emfConverterSvg', fallback = None) self.emfConverterSvg = config.get('media', 'emfConverterSvg', fallback = None)
...@@ -401,6 +401,15 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion: ...@@ -401,6 +401,15 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion:
_print(f'[yellow]{_symError}') _print(f'[yellow]{_symError}')
_result += f'<mark>{_symError}</mark>' _result += f'<mark>{_symError}</mark>'
# ignore deleted test
case 'del':
pass
# try to keep the text of inserted text
case 'ins':
for x in element:
_result += _parseXML(x)
case _ if tag in _ignoredTags: # ignore case _ if tag in _ignoredTags: # ignore
pass pass
...@@ -606,6 +615,8 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion: ...@@ -606,6 +615,8 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion:
elif style in docConfig.tablecaption: elif style in docConfig.tablecaption:
lines.append('') lines.append('')
lines.append(f'**{replaceNL(text).strip()}**') lines.append(f'**{replaceNL(text).strip()}**')
anchor = f'<a name="table_{caption[6:].split(":")[0].strip()}"></a>' if caption.startswith('Table ') and ':' in caption else ''
lines.append(f'**{caption}**{anchor}')
# Image Caption # Image Caption
elif style in docConfig.imagecaption: elif style in docConfig.imagecaption:
...@@ -775,14 +786,14 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion: ...@@ -775,14 +786,14 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion:
# Convert media files # Convert media files
# #
def _convertImage(converter:str, format:str): def _convertImage(converter:str, format:str) -> None:
if converter: if converter:
for fn in list(emfFiles): for fn in list(emfFiles):
_f = Path(fn) # Filename to handle _f = Path(fn) # Filename to handle
_t = imageDirName # Target directory _t = imageDirName # Target directory
if _f.stem not in referencedImages: if _f.stem not in referencedImages:
if not fn.startswith(unreferencedDirName): if not fn.startswith(unreferencedDirName):
_print(f'[red]Unreferenced image: {PurePath(fn).name} {"(skipped)" if docConfig.skipUnreferencedMediaFiles else ""}', highlight = False) _print(f'[yellow]Unreferenced image in the document: {PurePath(fn).name} {"(skipped)" if docConfig.skipUnreferencedMediaFiles else ""}', highlight = False)
_print(f'[yellow]Moving image file to: {unreferencedDirName}', highlight = False) _print(f'[yellow]Moving image file to: {unreferencedDirName}', highlight = False)
_n = f'{unreferencedDirName}/{_f.name}' _n = f'{unreferencedDirName}/{_f.name}'
_p = _f.replace(_n) _p = _f.replace(_n)
...@@ -796,7 +807,9 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion: ...@@ -796,7 +807,9 @@ def processDocuments(documents:list[str], outDirectory:str, skipImageConversion:
cmd = cmd.replace('{infile}', fn).replace('{outdir}', _t) cmd = cmd.replace('{infile}', fn).replace('{outdir}', _t)
_print(f'Converting EMF file: {fn} to "{format}"', highlight = False) _print(f'Converting EMF file: {fn} to "{format}"', highlight = False)
if (res := subprocess.run(cmd, shell = True, capture_output = True)).returncode != 0: if (res := subprocess.run(cmd, shell = True, capture_output = True)).returncode != 0:
_print(f'[red] Error running command: {res.stderr.decode("utf-8")}') _print(f'[red]Error running command: {res.stderr.decode("utf-8")}')
_print(f'[red]Please check the configuration file -> section "\[media]" for the converter command: {converter}')
break
if not skipImageConversion: if not skipImageConversion:
if docConfig.emfConverterPng: if docConfig.emfConverterPng:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment