diff options
author | Adam Mathes <adam@trenchant.org> | 2019-06-08 17:02:44 -0700 |
---|---|---|
committer | Adam Mathes <adam@trenchant.org> | 2019-06-08 17:02:44 -0700 |
commit | 97e935a1947291d6b422877906dbb75e22b05e2d (patch) | |
tree | 8471a258673a123add31afa20a96a2b29541f5a6 | |
parent | a5490ac27b5fe0d9f3404625844c82d3b6d3d612 (diff) | |
download | snkt-97e935a1947291d6b422877906dbb75e22b05e2d.tar.gz snkt-97e935a1947291d6b422877906dbb75e22b05e2d.tar.bz2 snkt-97e935a1947291d6b422877906dbb75e22b05e2d.zip |
some hacky regex url/img extraction stuff
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | post/post.go | 6 | ||||
-rw-r--r-- | render/render.go | 33 |
3 files changed, 38 insertions, 3 deletions
@@ -39,7 +39,7 @@ func main() { } if help { flag.Usage() - fmt.Printf("code and docs: https://github.com/adammathes/snkt \n") + fmt.Printf("code and docs: https://git.adammathes.com/snkt \n") return } diff --git a/post/post.go b/post/post.go index 7fa7e90..5860eee 100644 --- a/post/post.go +++ b/post/post.go @@ -41,6 +41,8 @@ type Post struct { InFuture bool WordCount int Tags []string + Urls []string + Imgs []string // Content text -- raw, unprocessed, unfiltered markdown Text string @@ -220,6 +222,10 @@ func (p *Post) parse() { p.Tags = append(p.Tags, NormalizeTag(tag)) } } + + // Images and URLs + p.Urls = render.FindURLs(p.AbsoluteContent) + p.Imgs = render.FindImgs(p.AbsoluteContent) } /* diff --git a/render/render.go b/render/render.go index bf53208..9698ad8 100644 --- a/render/render.go +++ b/render/render.go @@ -15,8 +15,7 @@ import ( var templates map[string]*template.Template var BASE_TEMPLATE = "base" -var rel_href *regexp.Regexp -var rel_src *regexp.Regexp +var rel_href, rel_src, re_href, re_src *regexp.Regexp /* Renderable interface - objects that render themeslves to a []byte and @@ -86,6 +85,8 @@ func Init() { } rel_href = regexp.MustCompile(`href="/(.+)"`) rel_src = regexp.MustCompile(`src="/(.+)"`) + re_href = regexp.MustCompile(`href="(.*?)"`) + re_src = regexp.MustCompile(`src="(.*?)"`) } /* @@ -130,6 +131,34 @@ func ResolveURLs(html, prefix string) string { } /* +Finds all URLs that are hrefs +TODO: replace noisy regex with HTML parser +*/ +func FindURLs(html string) []string { + // bts := []byte(html) + hrefs := re_href.FindAllStringSubmatch(html, -1) + var urls []string + for _, href := range hrefs { + urls = append(urls, href[1]) + } + return urls +} + +/* +Finds all img urls via img src tags +TODO: replace noisy regex with HTML parser +*/ +func FindImgs(html string) []string { + // bts := []byte(html) + srcs := re_src.FindAllStringSubmatch(html, -1) + var imgs []string + for _, src := range srcs { + imgs = append(imgs, src[1]) + } + return imgs +} + +/* Runs all regex filters specified in config.Config.Filters */ func Filter(txt []byte) []byte { |