-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
package lib | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"os" | ||
"errors" | ||
"flag" | ||
"os" | ||
) | ||
|
||
func getCommand(arguments []string) (string, error) { | ||
validCommands := []string{"update", "ls", "conf", "init"} | ||
for _, validCommand := range validCommands { | ||
for _, argument := range arguments { | ||
if argument == validCommand { | ||
return validCommand, nil | ||
} | ||
} | ||
} | ||
return "", errors.New("No valid command found. Use mdrss <<ls, update, conf, init>>") | ||
validCommands := []string{"update", "ls", "conf", "init"} | ||
for _, validCommand := range validCommands { | ||
for _, argument := range arguments { | ||
if argument == validCommand { | ||
return validCommand, nil | ||
} | ||
} | ||
} | ||
return "", errors.New("No valid command found. Use mdrss <<ls, update, conf, init>>") | ||
} | ||
|
||
func DefaultConfigPath() string { | ||
dirname, _ := os.UserHomeDir() | ||
return dirname + "/.mdrss/config.json" | ||
dirname, _ := os.UserHomeDir() | ||
return dirname + "/.mdrss/config.json" | ||
} | ||
|
||
func ParseArguments(arguments []string) (map[string]*string, error) { | ||
parsedArguments := make(map[string]*string) | ||
command, commandErr := getCommand(arguments) | ||
parsedArguments["config"] = flag.String("config", DefaultConfigPath(), "path to config.json") | ||
parsedArguments["command"] = &command | ||
flag.Parse() | ||
return parsedArguments, commandErr | ||
parsedArguments := make(map[string]*string) | ||
command, commandErr := getCommand(arguments) | ||
parsedArguments["config"] = flag.String("config", DefaultConfigPath(), "path to config.json") | ||
parsedArguments["command"] = &command | ||
flag.Parse() | ||
return parsedArguments, commandErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
package lib | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
) | ||
|
||
func FileExists(filename string) bool { | ||
if _, err := os.Stat(filename); err != nil { | ||
return false | ||
} | ||
return true | ||
if _, err := os.Stat(filename); err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func ReadConfig(configPath string) (Config, error) { | ||
var config Config | ||
if FileExists(configPath) { | ||
configContent, _ := os.ReadFile(configPath) | ||
jsonCfgErr := json.Unmarshal(configContent, &config) | ||
if jsonCfgErr != nil { | ||
return config, errors.New("Error when reading config file.") | ||
} | ||
return config, nil | ||
} | ||
return config, errors.New("Config file not found. Please add it at ~/.mdrss/config.json or run \"mdrss init\"") | ||
var config Config | ||
if FileExists(configPath) { | ||
configContent, _ := os.ReadFile(configPath) | ||
jsonCfgErr := json.Unmarshal(configContent, &config) | ||
if jsonCfgErr != nil { | ||
return config, errors.New("Error when reading config file.") | ||
} | ||
return config, nil | ||
} | ||
return config, errors.New("Config file not found. Please add it at ~/.mdrss/config.json or run \"mdrss init\"") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
package lib | ||
|
||
import ( | ||
"os" | ||
"time" | ||
"os" | ||
"time" | ||
) | ||
|
||
func addItem(xmlContent string, config Config, article Article) string { | ||
timestamp := article.DatePublished.Format(time.RFC822Z) | ||
xmlContent += "\t<item>\n" | ||
xmlContent += "\t\t" | ||
xmlContent = fillChannel(xmlContent, "author", config.Channel.Author) | ||
xmlContent += "\t\t<title>" + article.Title + "</title>\n" | ||
xmlContent += "\t\t<link>" + config.Link + "</link>\n" | ||
xmlContent += "\t\t<pubDate>" + timestamp + "</pubDate>\n" | ||
xmlContent += "\t\t<description><![CDATA[" + article.Description + "]]></description>\n" | ||
xmlContent += "\t</item>\n" | ||
return xmlContent | ||
timestamp := article.DatePublished.Format(time.RFC822Z) | ||
xmlContent += "\t<item>\n" | ||
xmlContent += "\t\t" | ||
xmlContent = fillChannel(xmlContent, "author", config.Channel.Author) | ||
xmlContent += "\t\t<title>" + article.Title + "</title>\n" | ||
xmlContent += "\t\t<link>" + config.Link + "</link>\n" | ||
xmlContent += "\t\t<pubDate>" + timestamp + "</pubDate>\n" | ||
xmlContent += "\t\t<description><![CDATA[" + article.Description + "]]></description>\n" | ||
xmlContent += "\t</item>\n" | ||
return xmlContent | ||
} | ||
|
||
func addHeader(config Config) string { | ||
timestamp := time.Now().Format(time.RFC822Z) | ||
xmlContent := "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" | ||
xmlContent += "<rss version=\"2.0\">\n" | ||
xmlContent += "<lastBuildDate>" + timestamp + "</lastBuildDate>\n" | ||
xmlContent += "<channel>\n" | ||
xmlContent += "<title>" + config.Description + "</title>\n" | ||
xmlContent += "<link>" + config.Link + "</link>\n" | ||
xmlContent += "<description>" + config.Description + "</description>\n" | ||
timestamp := time.Now().Format(time.RFC822Z) | ||
xmlContent := "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" | ||
xmlContent += "<rss version=\"2.0\">\n" | ||
xmlContent += "<lastBuildDate>" + timestamp + "</lastBuildDate>\n" | ||
xmlContent += "<channel>\n" | ||
xmlContent += "<title>" + config.Description + "</title>\n" | ||
xmlContent += "<link>" + config.Link + "</link>\n" | ||
xmlContent += "<description>" + config.Description + "</description>\n" | ||
|
||
xmlContent = fillChannel(xmlContent, "language", config.Channel.Language) | ||
xmlContent = fillChannel(xmlContent, "copyright", config.Channel.Copyright) | ||
xmlContent = fillChannel(xmlContent, "managingEditor", config.Channel.ManagingEditor) | ||
xmlContent = fillChannel(xmlContent, "webMaster", config.Channel.WebMaster) | ||
xmlContent = fillChannel(xmlContent, "category", config.Channel.Category) | ||
xmlContent = fillChannel(xmlContent, "generator", config.Channel.Generator) | ||
xmlContent = fillChannel(xmlContent, "docs", config.Channel.Docs) | ||
xmlContent = fillChannel(xmlContent, "cloud", config.Channel.Cloud) | ||
xmlContent = fillChannel(xmlContent, "ttl", config.Channel.Ttl) | ||
// Copy image into same folder as feed so server can serve it | ||
xmlContent = fillChannel(xmlContent, "image", config.Channel.Image) | ||
xmlContent = fillChannel(xmlContent, "rating", config.Channel.Rating) | ||
xmlContent = fillChannel(xmlContent, "skipHours", config.Channel.SkipHours) | ||
xmlContent = fillChannel(xmlContent, "skipDays", config.Channel.SkipDays) | ||
xmlContent = fillChannel(xmlContent, "language", config.Channel.Language) | ||
xmlContent = fillChannel(xmlContent, "copyright", config.Channel.Copyright) | ||
xmlContent = fillChannel(xmlContent, "managingEditor", config.Channel.ManagingEditor) | ||
xmlContent = fillChannel(xmlContent, "webMaster", config.Channel.WebMaster) | ||
xmlContent = fillChannel(xmlContent, "category", config.Channel.Category) | ||
xmlContent = fillChannel(xmlContent, "generator", config.Channel.Generator) | ||
xmlContent = fillChannel(xmlContent, "docs", config.Channel.Docs) | ||
xmlContent = fillChannel(xmlContent, "cloud", config.Channel.Cloud) | ||
xmlContent = fillChannel(xmlContent, "ttl", config.Channel.Ttl) | ||
// Copy image into same folder as feed so server can serve it | ||
xmlContent = fillChannel(xmlContent, "image", config.Channel.Image) | ||
xmlContent = fillChannel(xmlContent, "rating", config.Channel.Rating) | ||
xmlContent = fillChannel(xmlContent, "skipHours", config.Channel.SkipHours) | ||
xmlContent = fillChannel(xmlContent, "skipDays", config.Channel.SkipDays) | ||
|
||
return xmlContent | ||
return xmlContent | ||
} | ||
|
||
func fillChannel(xmlContent, tag, value string) string { | ||
if value != "" { | ||
return xmlContent + "<" + tag + ">" + value + "</" + tag + ">\n" | ||
} | ||
return xmlContent | ||
if value != "" { | ||
return xmlContent + "<" + tag + ">" + value + "</" + tag + ">\n" | ||
} | ||
return xmlContent | ||
} | ||
|
||
func CreateRSS(config Config) string { | ||
xmlContent := addHeader(config) | ||
for _, article := range config.Articles { | ||
if len(article.Title) != 0 { | ||
xmlContent = addItem(xmlContent, config, article) | ||
Info.Printf("Added '%s' to RSS feed. ", article.Title) | ||
} else { | ||
Warn.Printf("%s doesn't have a valid markdown title.", article.Filename) | ||
} | ||
} | ||
xmlContent += "</channel>\n</rss>\n" | ||
return xmlContent | ||
xmlContent := addHeader(config) | ||
for _, article := range config.Articles { | ||
if len(article.Title) != 0 { | ||
xmlContent = addItem(xmlContent, config, article) | ||
Info.Printf("Added '%s' to RSS feed. ", article.Title) | ||
} else { | ||
Warn.Printf("%s doesn't have a valid markdown title.", article.Filename) | ||
} | ||
} | ||
xmlContent += "</channel>\n</rss>\n" | ||
return xmlContent | ||
} | ||
|
||
func WriteRSS(rssContent string, config Config) error { | ||
rssByte := []byte(rssContent) | ||
err := os.WriteFile(config.OutputFile, rssByte, 0644) | ||
return err | ||
rssByte := []byte(rssContent) | ||
err := os.WriteFile(config.OutputFile, rssByte, 0644) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,46 @@ | ||
package lib | ||
|
||
import ( | ||
"regexp" | ||
"time" | ||
"regexp" | ||
Check failure on line 4 in lib/types.go GitHub Actions / unit tests
Check failure on line 4 in lib/types.go GitHub Actions / linter check
|
||
"time" | ||
) | ||
|
||
type Markdown struct { | ||
Content []Line | ||
} | ||
|
||
type Line struct { | ||
Link *regexp.Regexp | ||
UnorderedList *regexp.Regexp | ||
OrderedList *regexp.Regexp | ||
CodeBlock *regexp.Regexp | ||
|
||
// optional fields | ||
CodeBlockOpen bool | ||
UnorderedListActive bool | ||
OrderedListActive bool | ||
} | ||
|
||
type Article struct { | ||
Id int | ||
Title string | ||
Author string | ||
Filename string | ||
Description string | ||
DatePublished time.Time | ||
Id int | ||
Title string | ||
Author string | ||
Filename string | ||
Description string | ||
DatePublished time.Time | ||
} | ||
|
||
type Config struct { | ||
InputFolder string | ||
OutputFile string | ||
// Required for valid RSS | ||
Title string | ||
Link string | ||
Description string | ||
|
||
// optional fields | ||
Articles []Article | ||
Channel ChannelAttributes | ||
InputFolder string | ||
OutputFile string | ||
// Required for valid RSS | ||
Title string | ||
Link string | ||
Description string | ||
|
||
// optional fields | ||
Articles []Article | ||
Channel ChannelAttributes | ||
} | ||
|
||
type ChannelAttributes struct { | ||
// https://www.rssboard.org/rss-specification | ||
Author string | ||
Language string | ||
Copyright string | ||
ManagingEditor string | ||
WebMaster string | ||
Category string | ||
Generator string | ||
Docs string | ||
Cloud string | ||
Ttl string | ||
Image string | ||
Rating string | ||
SkipHours string | ||
SkipDays string | ||
// https://www.rssboard.org/rss-specification | ||
Author string | ||
Language string | ||
Copyright string | ||
ManagingEditor string | ||
WebMaster string | ||
Category string | ||
Generator string | ||
Docs string | ||
Cloud string | ||
Ttl string | ||
Image string | ||
Rating string | ||
SkipHours string | ||
SkipDays string | ||
} |