-
Notifications
You must be signed in to change notification settings - Fork 20
/
readme-converter.sh
executable file
·129 lines (112 loc) · 3.72 KB
/
readme-converter.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
################################################################################
# Convert readme files between WordPress Plugin readme and Github markdown format
# This script is used by the deploy-plugin.sh script
#
# Author: Sudar <http://sudarmuthu.com>
#
# License: Beerware ;)
#
# Usage:
# ./path/to/readme-converter.sh [from-file] [to-file] [format to-wp|from-wp]
#
# Refer to the README.md file for information about the different options
#
# Credit: Uses most of the code from the following places
# https://github.com/ocean90/svn2git-tools/
################################################################################
# wrapper for sed
_sed() {
# -E is used so that it is compatible in both Mac and Ubuntu.
sed -E "$1" $2 > $2.tmp && mv $2.tmp $2
}
# Check if file exists
file_exists () {
if [ ! -f $1 ]; then
echo "$1 doesn't exist"
exit 1;
fi
}
# Handle screenshots section for WP to Markdown format
ss_wptomd () {
awk '
BEGIN { # Set the field separator to a . for picking up line num
FS = "."
}
/^==/ { # If we hit a new section stop
flag = 0
}
NF && flag && $1 ~ /^[0-9]+$/ { # If the line is not empty and the flag is set add text
print "![](screenshot-" $1 ".png)"
sub(/^[0-9]+. */, "") # Remove the leading line number (no limit on fields)
}
/^== Screenshots ==/ { # If we hit the screenshot section start
flag = 1
}
{ # Print all the lines in the file
print
}
' $1 > $2
}
# Handle screenshots section for Markdown to WP format
ss_mdtowp () {
awk '
/^##/ { # If we hit a new section stop
flag = 0
}
NF && flag && $0 ~ /^!\[\]/ { # If the line contains a markdown image
total = split($0, arr, /[-.]/)
num = arr[total - 1]
next
}
flag && num && NF > 1 { # If we have a image number
print num ". " $0
num = 0
next
}
/^## Screenshots ##/ { # If we hit the screenshot section start
flag = 1
}
{ # Print all the lines in the file
print
}
' $1 > $2
}
# WP to Markdown format
wptomarkdown () {
file_exists $1
ss_wptomd $1 $2
PLUGINMETA=("Contributors" "Donate link" "Donate Link" "Tags" "Requires at least" "Requires PHP" "Tested up to" "Stable tag" "License" "License URI" "Requires base plugin" "Requires base plugin version")
for m in "${PLUGINMETA[@]}"
do
_sed 's/^'"$m"':/**'"$m"':**/g' $2
done
_sed "s/===([^=]+)===/#\1#/g" $2
_sed "s/==([^=]+)==/##\1##/g" $2
_sed "s/=([^=]+)=/###\1###/g" $2
}
# Markdown to WP format
markdowntowp () {
file_exists $1
ss_mdtowp $1 $2
PLUGINMETA=("Contributors" "Donate link" "Donate Link" "Tags" "Requires at least" "Requires PHP" "Tested up to" "Stable tag" "License" "License URI" "Requires base plugin" "Requires base plugin version")
for m in "${PLUGINMETA[@]}"
do
_sed 's/^(\*\*|__)'"$m"':(\*\*|__)/'"$m"':/g' $2
done
_sed "s/###([^#]+)###/=\1=/g" $2
_sed "s/##([^#]+)##/==\1==/g" $2
_sed "s/#([^#]+)#/===\1===/g" $2
_sed "/scrutinizer-ci/d" $2 # ignore links that contain scrutinizer-ci
}
if [ $# -eq 3 ]; then
if [ "$3" == "to-wp" ]; then
markdowntowp $1 $2
else
wptomarkdown $1 $2
fi
else
echo >&2 \
"usage: $0 [from-file] [to-file] [format to-wp|from-wp]"
exit 1;
fi