Sfoglia il codice sorgente

Add post title extraction

master
Dejvino 3 anni fa
parent
commit
0a86cfaee6
2 ha cambiato i file con 31 aggiunte e 9 eliminazioni
  1. +1
    -1
      config.sh.template
  2. +30
    -8
      post.sh

+ 1
- 1
config.sh.template Vedi File

@@ -1,6 +1,6 @@
#!/bin/bash
EDITOR=vim
TRANSFORM="" # '' / 'markdown'
TRANSFORM=('title' 'markdown') # empty or a subset of: 'title' 'markdown'
USER="" # WP user to create the post
PASSWORD="" # application password generated for your WP user
SERVER="" # server hostname, optionally with subdirectories


+ 30
- 8
post.sh Vedi File

@@ -3,7 +3,7 @@

# config:
EDITOR=vim
TRANSFORM="" # '' / 'markdown'
TRANSFORM=() # empty or a subset of: 'title' 'markdown'
USER="" # WP user to create the post
PASSWORD="" # application password generated for your WP user
SERVER="" # server hostname, optionally with subdirectories
@@ -19,19 +19,40 @@ $EDITOR $TMPFILE || exit 1
[[ -e $TMPFILE ]] || exit 1

# transformations
if [[ $TRANSFORM -eq "markdown" ]]; then
python >$TMPFILE.trans <<EOF
cp $TMPFILE $TMPFILE.trans
for T in "${TRANSFORM[@]}"; do
if [[ "$T" == "title" ]]; then
python >$TMPFILE.trans2 <<EOF
import re
with open('$TMPFILE.trans', 'r') as file:
title = ''
content = file.read()
matches = re.match('^#+ (.+)', content, flags=0)
if matches:
title = matches.group(1)
content = content[len(matches.group(0)) + 1:]
with open('$TMPFILE.title', 'w') as titlefile:
titlefile.write(title)
print(content)
EOF
TITLE=`cat $TMPFILE.title`
rm $TMPFILE.title
mv $TMPFILE.trans2 $TMPFILE.trans
elif [[ "$T" == "markdown" ]]; then
python >$TMPFILE.trans2 <<EOF
import markdown2
print(markdown2.markdown_path('$TMPFILE'))
print(markdown2.markdown_path('$TMPFILE.trans'))
EOF
CONTENT=`cat $TMPFILE.trans`
else
CONTENT=`cat $TMPFILE`
fi
mv $TMPFILE.trans2 $TMPFILE.trans
fi
done
CONTENT=`cat $TMPFILE.trans`
rm $TMPFILE.trans

echo "--- START POST ---"
echo $CONTENT
echo "--- END POST ---"
echo "Title: $TITLE"
echo "User: $USER"
echo "Server: $SERVER"
echo "Status: $STATUS"
@@ -42,6 +63,7 @@ read -p "Press enter to confirm..."

# push the post!
curl --user "$USER:$PASSWORD" -X POST \
--data-urlencode "title=$TITLE" \
--data-urlencode "content=$CONTENT" \
--data-urlencode "status=$STATUS" \
--data-urlencode "categories=$CATEGORIES" \


Caricamento…
Annulla
Salva