Commit d861a67a authored by Marco Cavalli's avatar Marco Cavalli
Browse files

feat: docker version of md tool converter

parent 5eded370
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
*.so
*.egg
*.egg-info/
dist/
build/
.git/
.gitignore
.vscode/
.idea/
*.md
!requirements.txt
.env
venv/
env/
GENERATED_FILES/*
!GENERATED_FILES/.gitkeep
 No newline at end of file
+57 −0
Original line number Diff line number Diff line
FROM python:3.10.16-slim

# Build argument for architecture (amd64 or arm64)
ARG TARGETARCH=amd64

# Install system dependencies
RUN apt-get update && apt-get install -y \
    wget \
    curl \
    git \
    libreoffice \
    imagemagick \
    && rm -rf /var/lib/apt/lists/*

# Install Pandoc 3.7.0.2 based on architecture
RUN if [ "$TARGETARCH" = "arm64" ]; then \
        wget https://github.com/jgm/pandoc/releases/download/3.7.0.2/pandoc-3.7.0.2-1-arm64.deb \
        && dpkg -i pandoc-3.7.0.2-1-arm64.deb \
        && rm pandoc-3.7.0.2-1-arm64.deb; \
    else \
        wget https://github.com/jgm/pandoc/releases/download/3.7.0.2/pandoc-3.7.0.2-1-amd64.deb \
        && dpkg -i pandoc-3.7.0.2-1-amd64.deb \
        && rm pandoc-3.7.0.2-1-amd64.deb; \
    fi

# Set working directory
WORKDIR /app

# Copy requirements first for better caching
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the entire application
COPY . .

# Create GENERATED_FILES directory
RUN mkdir -p GENERATED_FILES
RUN mkdir -p sources

# Create non-root user
RUN useradd -m -u 1000 appuser && \
    chown -R appuser:appuser /app

USER appuser

# Declare volume mount point
VOLUME /app/GENERATED_FILES
VOLUME /app/sources

# Set environment variables
ENV PYTHONUNBUFFERED=1

# Default entrypoint
ENTRYPOINT ["python"]
CMD ["convert.py"]
 No newline at end of file
+21 −0
Original line number Diff line number Diff line
@@ -253,3 +253,24 @@ Specify a different directory containing the HTML files.
[^1]: These steps may not be necessary with WSL 2, but it is recommended to follow them nevertheless.

[^2]: Method subject to change

# 3. Usage with Docker

## 3.1 Requirements

Install Docker on your local machine. The script will automatically build the Docker image and mount the necessary paths as required.

## 3.2 Run
To run the conversion with Docker, use the shell script appropriate for your operating system.

The accepted parameters are the same as those explained in [section 2.2](#22--conversion).

**For Mac/Linux:**

`bash convert.sh --parameters [--arch amd64|arm64]`

**For Windows:**

`./convert.bat --parameters [--arch amd64|arm64]`

Where `--parameters` are the same as those explained in [section 2.2](#22--conversion) and `--arch` is an optional parameter to specify the architecture of the Docker image to be built (default is `amd64`).
 No newline at end of file
+110 −0
Original line number Diff line number Diff line
@echo off
setlocal enabledelayedexpansion

set "ARCH=amd64"
set "FRM="
set "TO="
set "FOLDER="
set "SRC="
set "FILE_ORDER="

:parse_args
if "%~1"=="" goto args_done
if "%~1"=="--frm" (
	set "FRM=%~2"
	shift
	shift
	goto parse_args
)
if "%~1"=="--to" (
	set "TO=%~2"
	shift
	shift
	goto parse_args
)
if "%~1"=="--folder" (
	set "FOLDER=%~2"
	shift
	shift
	goto parse_args
)
if "%~1"=="--src" (
	set "SRC=%~2"
	shift
	shift
	goto parse_args
)
if "%~1"=="--file-order" (
	set "FILE_ORDER=%~2"
	shift
	shift
	goto parse_args
)
if "%~1"=="--arch" (
	set "ARCH=%~2"
	shift
	shift
	goto parse_args
)
echo Unknown parameter passed: %~1
exit /b 1

:args_done

if "%ARCH%" NEQ "amd64" if "%ARCH%" NEQ "arm64" (
	echo Error: --arch must be either 'amd64' or 'arm64'
	exit /b 1
)

rem Ensure the docker image exists; build if missing
docker image inspect md-converter >nul 2>&1
if errorlevel 1 (
	docker build --build-arg TARGETARCH=%ARCH% -t md-converter .
	if errorlevel 1 (
		echo Failed to build image md-converter.
		exit /b 1
	)
)

if "%FRM%"=="" (
	echo Error: --frm is required.
	exit /b 1
)
if "%TO%"=="" (
	echo Error: --to is required.
	exit /b 1
)
if "%FOLDER%"=="" (
	echo Error: --folder is required.
	exit /b 1
)

set "SRC_VOL=%FOLDER%"
if defined SRC set "SRC_VOL=%SRC%"

set "EXTRA_ARGS="
if defined SRC set "EXTRA_ARGS=!EXTRA_ARGS! --src ./sources"
if defined FILE_ORDER set "EXTRA_ARGS=!EXTRA_ARGS! --file-order ""%FILE_ORDER%"""

set "GEN_DIR=%CD%\GENERATED_FILES"

if defined EXTRA_ARGS (
	docker run ^
		-v "%GEN_DIR%:/app/GENERATED_FILES:rw" ^
		-v "%SRC_VOL%:/app/sources:rw" ^
		md-converter convert.py ^
		--frm "%FRM%" ^
		--to "%TO%" ^
		--folder "%FOLDER%" ^
		!EXTRA_ARGS!
) else (
	docker run ^
		-v "%GEN_DIR%:/app/GENERATED_FILES:rw" ^
		-v "%SRC_VOL%:/app/sources:rw" ^
		md-converter convert.py ^
		--frm "%FRM%" ^
		--to "%TO%" ^
		--folder "%FOLDER%"
)

endlocal
+2 −1
Original line number Diff line number Diff line
@@ -3,3 +3,4 @@ beautifulsoup4
cssutils
Pillow
lxml
docxcompose
 No newline at end of file