This commit is contained in:
hoangvv 2025-02-28 12:28:07 +07:00
parent e460e4553a
commit d606713b6b
3 changed files with 25 additions and 5 deletions

View File

@ -206,6 +206,7 @@ extensions = [
# Strange html domain logic used in memento pages # Strange html domain logic used in memento pages
"html_domain", "html_domain",
"myst_parser", "myst_parser",
"sphinx-design",
] ]
myst_enable_extensions = [ myst_enable_extensions = [

View File

@ -31,7 +31,6 @@ default_conversion: parse_all
conversions: conversions:
docutils.parsers.rst.directives.images.image: parse_all docutils.parsers.rst.directives.images.image: parse_all
sphinx.directives.patches.ListTable: parse_all # For tables if needed sphinx.directives.patches.ListTable: parse_all # For tables if needed
colon_fences.directives.card: parse_all
extensions: extensions:
- sphinx.ext.intersphinx - sphinx.ext.intersphinx
- sphinx.ext.todo - sphinx.ext.todo

View File

@ -9,7 +9,8 @@ SOURCE_DIR=$(realpath "$1") # Normalize source path
readonly TARGET_DIR="$2" readonly TARGET_DIR="$2"
readonly TEMP_ERROR=$(mktemp) readonly TEMP_ERROR=$(mktemp)
readonly LOG_FILE="/tmp/convert2md_$$.log" readonly LOG_FILE="/tmp/convert2md_$$.log"
trap 'rm -f "$TEMP_ERROR" "$LOG_FILE"' EXIT readonly UNPROCESSED_FILE="/tmp/unprocessed_$$.txt"
trap 'rm -f "$TEMP_ERROR" "$LOG_FILE" "$UNPROCESSED_FILE"' EXIT
validate_inputs() { validate_inputs() {
[[ ! -d "$SOURCE_DIR" ]] && { echo "Error: Source directory '$SOURCE_DIR' does not exist." >&2; exit 1; } [[ ! -d "$SOURCE_DIR" ]] && { echo "Error: Source directory '$SOURCE_DIR' does not exist." >&2; exit 1; }
@ -23,11 +24,12 @@ process_rst_file() {
local md_file_name="${relative_path%.rst}.md" local md_file_name="${relative_path%.rst}.md"
local target_file="$TARGET_DIR/$md_file_name" local target_file="$TARGET_DIR/$md_file_name"
local target_dir=$(dirname "$target_file") local target_dir=$(dirname "$target_file")
mkdir -p "$target_dir" || { echo "Error: Could not create '$target_dir' for '$rst_file'" >&2; return 1; } mkdir -p "$target_dir" || { echo "Error: Could not create '$target_dir' for '$rst_file'" >&2; echo "$rst_file" >> "$UNPROCESSED_FILE"; return 1; }
if rst2myst stream --config config.yaml "$rst_file" > "$target_file" 2>>"$TEMP_ERROR"; then if rst2myst stream --config config.yaml "$rst_file" > "$target_file" 2>>"$TEMP_ERROR"; then
# Replace 'rst-class' with 'container' and '.rst' with '.md' in content # Replace 'rst-class' with 'container' and '.rst' with '.md' in content
sed -i 's/rst-class/container/g; s/\.rst/.md/g' "$target_file" 2>>"$TEMP_ERROR" || { sed -i 's/rst-class/container/g; s/\.rst/.md/g' "$target_file" 2>>"$TEMP_ERROR" || {
echo "Error: Failed to modify content of '$target_file'" >&2 echo "Error: Failed to modify content of '$target_file'" >&2
echo "$rst_file" >> "$UNPROCESSED_FILE"
return 1 return 1
} }
echo "Converted: $rst_file -> $target_file" | tee -a "$LOG_FILE" echo "Converted: $rst_file -> $target_file" | tee -a "$LOG_FILE"
@ -35,6 +37,7 @@ process_rst_file() {
else else
echo "Failed to convert: $rst_file" >&2 echo "Failed to convert: $rst_file" >&2
cat "$TEMP_ERROR" >&2 cat "$TEMP_ERROR" >&2
echo "$rst_file" >> "$UNPROCESSED_FILE"
return 1 return 1
fi fi
} }
@ -48,12 +51,14 @@ copy_non_rst_file() {
# Replace 'rst-class' with 'container' and '.rst' with '.md' in content # Replace 'rst-class' with 'container' and '.rst' with '.md' in content
sed -i 's/rst-class/container/g; s/\.rst/.md/g' "$target_file" 2>>"$TEMP_ERROR" || { sed -i 's/rst-class/container/g; s/\.rst/.md/g' "$target_file" 2>>"$TEMP_ERROR" || {
echo "Error: Failed to modify content of '$target_file'" >&2 echo "Error: Failed to modify content of '$target_file'" >&2
echo "$file" >> "$UNPROCESSED_FILE"
return 1 return 1
} }
echo "Copied: $file -> $target_file" | tee -a "$LOG_FILE" echo "Copied: $file -> $target_file" | tee -a "$LOG_FILE"
return 0 return 0
} || { } || {
echo "Failed to copy: $file" >&2 echo "Failed to copy: $file" >&2
echo "$file" >> "$UNPROCESSED_FILE"
return 1 return 1
} }
} }
@ -69,9 +74,12 @@ main() {
printf '%s\n' "${RST_FILES[@]}" | sed 's/^/RST: /' >> "$LOG_FILE" printf '%s\n' "${RST_FILES[@]}" | sed 's/^/RST: /' >> "$LOG_FILE"
printf '%s\n' "${NON_RST_FILES[@]}" | sed 's/^/Non-RST: /' >> "$LOG_FILE" printf '%s\n' "${NON_RST_FILES[@]}" | sed 's/^/Non-RST: /' >> "$LOG_FILE"
# Initialize unprocessed file list with all found files
printf '%s\n' "${RST_FILES[@]}" "${NON_RST_FILES[@]}" > "$UNPROCESSED_FILE"
local parallel_jobs=$(( $(nproc) / 2 )) local parallel_jobs=$(( $(nproc) / 2 ))
export -f process_rst_file copy_non_rst_file export -f process_rst_file copy_non_rst_file
export SOURCE_DIR TARGET_DIR TEMP_ERROR LOG_FILE export SOURCE_DIR TARGET_DIR TEMP_ERROR LOG_FILE UNPROCESSED_FILE
# Process RST files # Process RST files
printf '%s\0' "${RST_FILES[@]}" | xargs -0 -P "$parallel_jobs" -I {} bash -c 'process_rst_file "{}"' || { printf '%s\0' "${RST_FILES[@]}" | xargs -0 -P "$parallel_jobs" -I {} bash -c 'process_rst_file "{}"' || {
@ -93,6 +101,18 @@ main() {
echo "RST files failed: $failed" | tee -a "$LOG_FILE" echo "RST files failed: $failed" | tee -a "$LOG_FILE"
echo "Non-RST files copied: $copied" | tee -a "$LOG_FILE" echo "Non-RST files copied: $copied" | tee -a "$LOG_FILE"
# List unprocessed files (those still in UNPROCESSED_FILE after processing)
if [[ -s "$UNPROCESSED_FILE" ]]; then
echo "Files not processed:" | tee -a "$LOG_FILE"
cat "$UNPROCESSED_FILE" | while IFS= read -r file; do
echo " $file" | tee -a "$LOG_FILE"
done
echo "Note: Some files were not processed. See above list and $LOG_FILE for details." >&2
exit 1
else
echo "All files processed successfully." | tee -a "$LOG_FILE"
fi
if [[ ${#RST_FILES[@]} -ne $((successful + failed)) ]]; then if [[ ${#RST_FILES[@]} -ne $((successful + failed)) ]]; then
echo "Error: ${#RST_FILES[@]} RST files found, but only $((successful + failed)) processed!" >&2 echo "Error: ${#RST_FILES[@]} RST files found, but only $((successful + failed)) processed!" >&2
exit 1 exit 1