Pt:Tutorial:Copiar conteúdo do fórum para o wiki/osm-bb2wiki.sh

From OpenStreetMap Wiki
Jump to navigation Jump to search

O código funciona se o tutorial for seguido à risca. Mas ele precisa ser indentado, comentado, receber tratamento de erros, e muito mais.

Atenção! O nome osm-bb2wiki.sh acabou perdendo o sentido, já que a conversão não é feita a partir de BBCode mas sim de HTML.

Lista de Coisas a Fazer

OOjs UI icon check-constructive.svg Indentar
Red x.svg  Documentar
Red x.svg  Tratamento da entrada
Red x.svg  Expansão da dica de ajuda help
Red x.svg  Opção de reduzir-se em begin e end
Red x.svg  html2wiki com --base-uri e --wiki-uri

Código

#!/bin/bash

function help() {
    echo " bash osm-bb2wiki.sh PROJETO"
    echo " bash osm-bb2wiki.sh PROJETO export"
    echo " bash osm-bb2wiki.sh PROJETO list"
    echo " bash osm-bb2wiki.sh PROJETO download"
    echo " bash osm-bb2wiki.sh PROJETO get 5"
    echo " bash osm-bb2wiki.sh PROJETO write"
    echo " bash osm-bb2wiki.sh PROJETO rename"
    echo " bash osm-bb2wiki.sh PROJETO pages"
}

if [ "$1" == "help" ]; then
    help
    exit 0
fi

html2wiki --dialect MediaWiki "$1.txt" > "$1-wiki.txt"

# <span class="postimg">[[Image:kcYPiud.png|kcYPiud.png]]</span>

function test_del_span() {
    grep "\[\[Image\:" "$1-wiki.txt" | 
      sed "s/<span class=\"[^\"]*\">\(\[\[Image:.*\]\]\)<\/span>/\1/g"
}

function del_span() {
    sed -i "s/<span class=\"[^\"]*\">\(\[\[Image:.*\]\]\)<\/span>/\1/g" "$1-wiki.txt"
}

function export_links() {
    sed -e "s/.*\(\[\[Image:.*\]\]\).*/\1\n/g" "$1-wiki.txt" |
      grep  "\[\[Image:" | 
      sed "s/^.*Image:\([^|]*\)|.*$/\1|NOME|DPT|DEN|LICENSE/g" > $1-files.txt
}

function view_links() {
    grep -n --color=always -e "NOME" -e "DEN" -e "DPT" -e "LICENSE" "$1-files.txt" | less -R
}

# PD
# PD-self
# PD-creator
# CC-by-sa-2.0
# File:Tutorial-restricoes-07-exemplo-01-restricoes-implicitas.png

function get_file_page_code() {
echo "
/* $1 */
== Summary ==
$3

$2
== Licensing ==
{{$4}}
"
}

function get_code() {
    TMP=$(grep -n "^" "$1-files.txt" | 
      grep "^$2:" | 
      cut -d ":" -f 2-)
    NAME=$(echo "$TMP" | cut -d "|" -f 2)
    DEN=$(echo "$TMP" | cut -d "|" -f 3)
    DPT=$(echo "$TMP" | cut -d "|" -f 4)
    LICENSE=$(echo "$TMP" | cut -d "|" -f 5-)
    get_file_page_code "$NAME" "$DEN" "$DPT" "$LICENSE" | less -R
}

function file_pages_code() {
    while read LINE; do
        if [ "`echo $LINE | grep "|"`" != "" ]; then
            NAME1=$(echo "$LINE" | cut -d "|" -f 1)
            NAME2=$(echo "$LINE" | cut -d "|" -f 2)
            DEN=$(echo "$LINE" | cut -d "|" -f 3)
            DPT=$(echo "$LINE" | cut -d "|" -f 4)
            LICENSE=$(echo "$LINE" | cut -d "|" -f 5-)
            echo >> "$1-pages.txt"
            get_file_page_code "$NAME2$NAME1" "$DEN" "$DPT" "$LICENSE" >> "$1-pages.txt"
            echo >> "$1-pages.txt"
        fi
    done < "$1-files.txt"
}

function write_links() {
    while read LINE; do
        if [ "`echo $LINE | grep "|"`" != "" ]; then
            NAME1=$(echo "$LINE" | cut -d "|" -f 1)
            NAME2=$(echo "$LINE" | cut -d "|" -f 2)
            sed -i "s/\[\[Image:$NAME1/\[\[Image:$NAME2/g" "$1-wiki-final.txt"
        fi
    done < "$1-files.txt"
}

function download() {
    while read LINE; do
        if [ "`echo $LINE | grep "|"`" != "" ]; then
            NAME1=$(echo "$LINE" | cut -d "|" -f 1)
            wget -P "download" "http://i.imgur.com/$NAME1"
        fi
    done < "$1-files.txt"
}

function rename() {
    mkdir -p "upload"
    while read LINE; do
        if [ "`echo $LINE | grep "|"`" != "" ]; then
            NAME1=$(echo "$LINE" | cut -d "|" -f 1)
            NAME2=$(echo "$LINE" | cut -d "|" -f 2)
            cp "download/$NAME1" "upload/$NAME2"
        fi
    done < "$1-files.txt"
}

del_span "$1"

if [ "$2" == "export" ]; then
    export_links "$1"
fi

if [ "$2" == "list" ]; then
    view_links "$1"
fi

if [ "$2" == "get" ]; then
    get_code "$1" "$3"
fi

if [ "$2" == "download" ]; then
    download "$1"
fi

if [ "$2" == "rename" ]; then
    rename "$1"
fi

if [ "$2" == "write" ]; then
    cp "$1-wiki.txt" "$1-wiki-final.txt"
    write_links "$1"
fi

if [ "$2" == "pages" ]; then
    file_pages_code "$1"
fi