diff --git a/document/main.go b/document/main.go
index 95f1352..19d94d0 100644
--- a/document/main.go
+++ b/document/main.go
@@ -4,0 +5 @@ import (
+ "fmt"
@@ -20 +21 @@ type Doc struct {
-func Write(path string) Doc {
+func Parse(path string) Doc {
@@ -22 +23,3 @@ func Write(path string) Doc {
- check(err)
+ if err != nil {
+ fmt.Println("Error opening file:", err)
+ }
@@ -32 +35,3 @@ func Write(path string) Doc {
- check(scanner.Err())
+ if err := scanner.Err(); err != nil {
+ fmt.Println("Error reading file:", err)
+ }
@@ -35 +40,3 @@ func Write(path string) Doc {
- check(err)
+ if err != nil {
+ fmt.Println("Error parsing HTML:", err)
+ }
@@ -40,0 +48,11 @@ func Write(path string) Doc {
+ // Print the results
+ fmt.Println("Stylesheet Links:")
+ for _, link := range stylesheets {
+ fmt.Println(link)
+ }
+
+ fmt.Println("\nStyle Tags:")
+ for _, style := range styleTags {
+ fmt.Println(style)
+ }
+
@@ -123,6 +140,0 @@ func localizePath(rootPath, filePath string) string {
-
-func check(e error) {
- if e != nil {
- panic(e)
- }
-}
package document
import (
"bufio"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/go-shiori/dom"
"golang.org/x/net/html"
)
type Doc struct {
StyleSheets []string
StyleTags []string
DOM *html.Node
}
func Write(path string) Doc {
file, err := os.Open(path)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
var htmlContent string
for scanner.Scan() {
htmlContent += scanner.Text() + "\n"
}
check(scanner.Err())
doc, err := dom.FastParse(strings.NewReader(htmlContent))
check(err)
// Extract stylesheet link tags and style tags
stylesheets := extractStylesheets(doc, filepath.Dir(path))
styleTags := extractStyleTags(doc)
d := Doc{
StyleSheets: stylesheets,
StyleTags: styleTags,
DOM: doc,
}
return d
}
func extractStylesheets(n *html.Node, baseDir string) []string {
var stylesheets []string
var dfs func(*html.Node)
dfs = func(node *html.Node) {
if node.Type == html.ElementNode && node.Data == "link" {
var href string
isStylesheet := false
for _, attr := range node.Attr {
if attr.Key == "rel" && attr.Val == "stylesheet" {
isStylesheet = true
} else if attr.Key == "href" {
href = attr.Val
}
}
if isStylesheet {
resolvedHref := localizePath(baseDir, href)
stylesheets = append(stylesheets, resolvedHref)
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
dfs(c)
}
}
dfs(n)
return stylesheets
}
func extractStyleTags(n *html.Node) []string {
var styleTags []string
var dfs func(*html.Node)
dfs = func(node *html.Node) {
if node.Type == html.ElementNode && node.Data == "style" {
var styleContent strings.Builder
for c := node.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
styleContent.WriteString(c.Data)
}
}
styleTags = append(styleTags, styleContent.String())
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
dfs(c)
}
}
dfs(n)
return styleTags
}
func localizePath(rootPath, filePath string) string {
// Check if the file path has a scheme, indicating it's a URL
u, err := url.Parse(filePath)
if err == nil && u.Scheme != "" {
return filePath
}
// Join the root path and the file path to create an absolute path
absPath := filepath.Join(rootPath, filePath)
// If the absolute path is the same as the original path, return it
if absPath == filePath {
return filePath
}
return "./" + absPath
}
func check(e error) {
if e != nil {
panic(e)
}
}