Skip to content

Instantly share code, notes, and snippets.

@t-yuki
Last active April 2, 2019 05:25
Show Gist options
  • Save t-yuki/7989449 to your computer and use it in GitHub Desktop.
Save t-yuki/7989449 to your computer and use it in GitHub Desktop.
1. a script to grab redmine wiki pages in markdown format and convert to standard markdown link format. 2. Makefile to convert markdown to sphinx reST.
#!/bin/bash
#
if [ -z "$APIKEY" ]; then
echo "API Key for Redmine is required."
exit 1
fi
mkdir -p doc/specs
cd doc/specs
for url in `cat urls.txt`; do
file=${url##*/}
md=${file%.txt}.md
tmp=${file%.txt}.xml
puturl=${url%.txt}.xml
echo PUT "${file%.txt}.md" $puturl
cat <<"EOF" > "$tmp"
<?xml version="1.0" encoding="UTF-8"?>
<wiki_page>
EOF
echo -n '<text><![CDATA[' >> "$tmp"
cat "$md" >> "$tmp"
echo ']]></text>' >> "$tmp"
cat <<"EOF" >> "$tmp"
</wiki_page>
EOF
sed -r -e 's/\[(.*)\]\((.*).md#(.*)\)/[[\2#\3|\1]]/g' -i "$tmp"
sed -r -e 's/\[(.*)\]\((.*).md\)/[[\2|\1]]/g' -i "$tmp"
perl -i -pe 'while($_ =~ m/\[\[(.*)\|(.*)\]\]/g){ if($1 eq $2) { s/\[\[(.*)\|(.*)\]\]/[[$1]]/; } } ' "$tmp"
sed -r -e '/\[CDATA\[/,/^.+$/ { s/^$//g; T; d; }' -i "$tmp"
curl -H "X-Redmine-API-Key: $APIKEY" -H "Content-type: application/xml" -X PUT --data-binary @"$tmp" $puturl
rm -f "$tmp"
done
#!/bin/bash
APIKEY=XXX
URL=http://example.com/projects/sandbox/wiki/Wiki.html
wget -l 4 -r -L -np -nd -nv -e robots=off --delete-after --header="X-Redmine-API-Key: $APIKEY" $URL 2>&1 | \
sed "/URL:/!d; s/^.*URL://g; s/\.html .*/.txt/g" | tee urls.txt
for url in `cat urls.txt`; do
file=${url##*/}
md=${file%.txt}.md
echo wget -O "${file%.txt}.md" $url
wget -q --header="X-Redmine-API-Key: $APIKEY" -O "$md" $url
sed -e 's/\r//g' -i "$md"
sed -r -e 's/\[\[([^[]*)#([^#]*)\|([^|]*)\]\]/[\3](\1.md#\2)/g' -i "$md"
sed -r -e 's/\[\[([^[]*)\|([^|]*)\]\]/[\2](\1.md)/g' -i "$md"
sed -r -e 's/\[\[([^[]*)\]\]/[\1](\1.md)/g' -i "$md"
echo >> "$md"
done
/begintoctree/,/endtoctree/ {
/^ *$/ n;
/begintoctree/ { s/.*//; h; d; };
/:ref:`.* <.*.md>`/ H;
/endtoctree/ {
i .. toctree:: \
:hidden:
x;
s/^.*:ref:`(.*) <(.*).md>`.*$/ \2/gm;
};
};
MD = $(wildcard *.md)
FILES = $(MD:%.md=%.rst)
.PHONY: rst
rst: intralink.txt $(FILES)
intralink.txt: $(MD)
truncate -s 0 $@
.md.rst: intralink.txt
@echo -n converting $< to $@
@truncate -s 0 $@
@echo ".. _$<:" >> $@
@echo >> $@
@pandoc -f markdown_github -t rst < $< >> $@
@sed -i -e 's/{{toc}}/.. contents::/g' $@
@touch intralink.txt
@cp -f -L intralink.txt intralink.txt.$$PPID
@grep -e '|$$' $@ | sed -r -e 's/.*`([^`]*) <(.*).md>`__/.. |\1 <\2.md>__| replace:: :ref:`\1 <\2.md>`\n/g; t X; d; : X p;' >> intralink.txt.$$PPID
@sed -i -e '/replace::/!d' intralink.txt.$$PPID
@sed -i -e 's/\\_/_/g' intralink.txt.$$PPID
@sort intralink.txt.$$PPID | uniq > intralink.txt
@rm -f intralink.txt.$$PPID
@sed -i -r -f intralink.sed $@
@echo ".. include:: intralink.txt" >> $@
@sed -i -r -e 's/:(\w+):``(.*)``/:\1:`\2`/' $@
@sed -i -e '/| {{rest()/,/| }}/ { /rest()/ d; /}}/ d; s/^| \.\./../g; s/^| */ /g}' $@
@sed -i -r -f toctree.sed $@
@echo " done"
{{rest()
.. md documentation master file
}}
Welcome to md's documentation!
==============================
Contents:
{{rest()
.. toctree::
:maxdepth: 4
README
test
test2
test3
}}
{{begintoctree}}
* [abc](test3-sub.md)
{{endtoctree}}
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
.. _index.md:
.. md documentation master file
Welcome to md's documentation!
==============================
Contents:
.. toctree::
:maxdepth: 4
README
test
test2
test3
- :ref:`abc <test3-sub.md>`
.. toctree::
:hidden:
test3-sub
Indices and tables
==================
- :ref:`genindex`
- :ref:`modindex`
- :ref:`search`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment