mastoGem/util.go

84 lines
2.3 KiB
Go

/*
MastoGem, A Mastodon proxy for Gemini
Copyright (C) 2021 Romain de Laage
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"html"
"regexp"
"strings"
"os"
"io/ioutil"
"encoding/json"
"log"
)
func getConfig() Config {
configPath := os.Getenv("MASTOGEM_CONFIG_PATH")
if configPath == "" {
log.Println("MASTOGEM_CONFIG_PATH was not set, using default settings")
config := Config{
Listen: "127.0.0.1:1965",
CertPath: "certs/cert.pem",
KeyPath: "certs/key.rsa",
BaseURL: "https://mamot.fr",
Title: "MastoGem",
HomeMessage: "Welcome on MastoGem, this is a Mastodon proxy for Gemini. You can view the last 20 toots of a Mastodon account by providing its ID.",
}
return config
}
configFile, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalln("config file: %s", err)
}
var config Config
json.Unmarshal(configFile, &config)
return config
}
func removeHTMLTags(content string) string {
text := strings.ReplaceAll(content, "<p>", "")
text = strings.ReplaceAll(text, "</p>", "\n\n")
text = strings.ReplaceAll(text, "<br />", "\n")
text = strings.ReplaceAll(text, "<br>", "\n")
text = strings.ReplaceAll(text, "</a>", "")
text = strings.ReplaceAll(text, "</span>", "")
regexString := "<a( [^>]*)?>"
regex, err := regexp.Compile(regexString)
if err != nil {
log.Println("regex: %s", err)
return ""
}
text = regex.ReplaceAllLiteralString(text, "")
regexString = "<span( [^>]*)?>"
regex, err = regexp.Compile(regexString)
if err != nil {
log.Println("regex: %s", err)
return ""
}
text = regex.ReplaceAllLiteralString(text, "")
text = html.UnescapeString(text)
return text
}