Skip to content
Snippets Groups Projects
Commit bb5de7bf authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Support to convert all svg figures to png for pandoc conversion

parent 53205db2
No related branches found
No related tags found
No related merge requests found
Pipeline #1174 passed
......@@ -28,6 +28,7 @@ Build pythonForPandocFilter docker image:
- generateChangemarks/changemarks.py
- generateChangemarks/addTrackedChanges.py
- generateChangemarks/generateTOC.py
- generateChangemarks/svg2png.py
Build Pandoc docker image:
stage: build
......
......@@ -2,6 +2,7 @@ FROM python:3.9-slim-bullseye
ADD . /generateChangemarks/
RUN apt-get update -y && \
apt-get install -y libcairo2 && \
rm -rf /var/lib/apt/lists/* &&\
pip install -e generateChangemarks/ &&\
pip install -r generateChangemarks/requirements.txt
FROM pandoc/core:3-ubuntu
FROM pandoc/core:3.4.0-ubuntu
RUN apt-get update -y && \
apt-get install -y npm &&\
......
......@@ -127,6 +127,26 @@ def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]:
return _lines
def replaceFiguresPathSvgToPng(progress: Progress, mdLines: list[str]) -> list[str]:
""" Replace figure extensions from svg to png.
"""
_taskID = progress.add_task('[blue]Replacing figure captions', total=0)
# progress.update()
figurePathRegex = re.compile('media\/.*\.svg')
_lines: list[str] = []
for line in mdLines:
matches = re.findall(figurePathRegex, line)
if matches:
# Replace figure path to png
_lines.append(re.sub(r'\.svg', f'.png', line))
else:
_lines.append(line)
progress.stop_task(_taskID)
return _lines
def replaceLineBreaks(progress: Progress, mdLines: list[str]) -> list[str]:
""" Replace <br /> linebreaks by pandoc escaped_line_breaks extension \(newline).
"""
......@@ -162,6 +182,7 @@ def process(document:str, outDirectory:str) -> None:
mdLines = correctTOC(progress, mdLines)
mdLines = replaceTableCaptions(progress, mdLines)
mdLines = replaceFigureCaptions(progress, mdLines)
mdLines = replaceFiguresPathSvgToPng(progress, mdLines)
mdLines = replaceLineBreaks(progress, mdLines)
writeMDFile(progress, mdLines, document, outDirectory)
......
......@@ -16,3 +16,4 @@ rich==13.4.2
# via oneM2M-markdown-to-pandoc-filter (setup.py)
requests==2.31.0
unidiff==0.7.5
cairosvg==2.7.1
\ No newline at end of file
......@@ -16,6 +16,7 @@ setup(
'changemarks=changemarks:main',
'addTrackedChanges=addTrackedChanges:main',
'generateTOC=generateTOC:main',
'svg2png=svg2png:main',
]
}
......
import argparse, sys
import cairosvg
import os
def svg_to_png(input_svg_path, output_png_path):
cairosvg.svg2png(url=input_svg_path, write_to=output_png_path)
# Example usage
def main(args=None):
# Parse command line arguments
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('image', help = 'image to convert')
args = parser.parse_args()
svg_to_png(args.image, f'{args.image.removesuffix(".svg")}.png')
if __name__ == '__main__':
sys.exit(main())
\ No newline at end of file
......@@ -27,6 +27,11 @@ if [ ! $specs ] ; then
exit 0
fi
for i in media/*.svg ; do
echo "\n------ Converting SVG to PNG for pandoc --------"
docker run --rm -v $(pwd):/tmp/ -u $(id -u):$(id -g) "$TOOLS_DOCKER_IMAGE" svg2png "/tmp/$i"
done
for i in *.md ; do
if [ $i != 'README.md' ]; then
echo "\n------ Adding TOC to spec --------"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment