-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2org.sh
executable file
·74 lines (64 loc) · 2 KB
/
md2org.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/env sh
# Check if input file is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 input_file.md"
exit 1
fi
# Check if input file exists
if [ ! -f "$1" ]; then
echo "Input file not found: $1"
exit 1
fi
# Set output file name
input_file="$1"
output_file="${input_file%.md}.org"
# Function to convert date string to org-mode format
convert_date() {
local input_date="$1"
local output_date
if date --version >/dev/null 2>&1; then
# GNU date command (Linux)
output_date=$(date -d "$input_date" +"[%Y-%m-%d %a %H:%M]")
else
# BSD date command (macOS)
output_date=$(date -j -f "%d %B %Y %I:%M:%S %p" "$input_date" +"[%Y-%m-%d %a %H:%M]")
fi
echo "$output_date"
}
# Start the conversion process
{
line_number=1
while IFS= read -r line; do
if [[ $line_number -eq 1 ]]; then
# First line (title with "#+title:")
echo "#+title:${line#\#}"
elif [[ $line_number -eq 2 ]]; then
# Second line (author with "#+author:" and without "#")
echo "#+author:${line//[#]/}"
else
case "$line" in
\#\ *)
echo "*${line#\#}"
;;
\#\#\ *)
echo "*${line#\#\#}"
;;
\#\#\#\ *)
# Check for a date after the "@"
if [[ "$line" =~ @\ (.*) ]]; then
# Extract the date and reformat it for org-mode
org_date=$(convert_date "${BASH_REMATCH[1]}")
# Replace the date in the line
line="${line/@ ${BASH_REMATCH[1]}/$org_date}"
fi
echo "**${line#\#\#\#}"
;;
*)
echo "$line"
;;
esac
fi
((line_number++))
done
} < "$input_file" > "$output_file"
echo "Conversion complete. Output file: $output_file"