Skip to content

Commit

Permalink
feat: copy content/ directories recursively
Browse files Browse the repository at this point in the history
Co-authored-by: Aditya Hegde <[email protected]>
  • Loading branch information
anirudhsudhir and bwaklog committed Apr 22, 2024
1 parent 293aed7 commit 66c653f
Show file tree
Hide file tree
Showing 18 changed files with 40 additions and 87 deletions.
7 changes: 3 additions & 4 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ func (cmd *Cmd) VanillaRender() {
e.DeepDataMerge.LinkStore = make(map[template.URL][]*parser.Note, 10)

helper := helpers.Helper{
ErrorLogger: e.ErrorLogger,
SiteDataPath: helpers.SiteDataPath,
ErrorLogger: e.ErrorLogger,
}

helper.CreateRenderedDir(helper.SiteDataPath)
helper.CreateRenderedDir(helpers.SiteDataPath)

p.ParseConfig(helpers.SiteDataPath + "layout/config.yml")
p.ParseRobots(helpers.SiteDataPath+"layout/robots.txt", helpers.SiteDataPath+"rendered/robots.txt")
Expand Down Expand Up @@ -72,7 +71,7 @@ func (cmd *Cmd) VanillaRender() {
e.GenerateJSONIndex(helpers.SiteDataPath)

e.GenerateLinkStore()
e.GenerateNoteJSONIdex(helper.SiteDataPath)
e.GenerateNoteJSONIdex(helpers.SiteDataPath)

templ, err := template.ParseGlob(helpers.SiteDataPath + "layout/*.html")
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions pkg/helpers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/cheggaaa/pb/v3"
)
Expand All @@ -17,8 +18,7 @@ const SiteDataPath string = "site/"
var version string = "2.0.0" // use variable

type Helper struct {
ErrorLogger *log.Logger
SiteDataPath string
ErrorLogger *log.Logger
}

// Copies the contents of the dirPath directory to outDirPath
Expand Down Expand Up @@ -50,6 +50,19 @@ func (h *Helper) CopyFiles(srcPath string, destPath string) {
}
defer source.Close()

// Creating subdirectories if the filepath contains '/'
if strings.Contains(string(destPath), "/") {
// Extracting the directory path from the page path
splitPaths := strings.Split(string(destPath), "/")
filename := splitPaths[len(splitPaths)-1]
pagePathWithoutFilename, _ := strings.CutSuffix(string(destPath), filename)

err := os.MkdirAll(pagePathWithoutFilename, 0750)
if err != nil {
h.ErrorLogger.Fatal(err)
}
}

destination, err := os.Create(destPath)
if err != nil {
h.ErrorLogger.Fatal(err)
Expand Down
23 changes: 9 additions & 14 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,17 @@ type Parser struct {
}

func (p *Parser) ParseMDDir(baseDirPath string, baseDirFS fs.FS) {
helper := helpers.Helper{
ErrorLogger: p.ErrorLogger,
}
fs.WalkDir(baseDirFS, ".", func(path string, dir fs.DirEntry, err error) error {
if path != "." && path != ".obsidian" {
if dir.IsDir() {
subDir := os.DirFS(path)
p.ParseMDDir(path, subDir)
} else {
fileName := strings.TrimPrefix(path, baseDirPath)
if filepath.Ext(path) == ".md" {
// OLD IMPL
// fileName := filepath.Base(path)
//
// NEW IMPL
// /contents/notes/2134321.md ==> notes/2134321.md
fileName := strings.TrimPrefix(path, baseDirPath)
// fmt.Println(fileNameWithPath, fileName)

content, err := os.ReadFile(baseDirPath + path)
if err != nil {
p.ErrorLogger.Fatal(err)
Expand All @@ -118,7 +114,8 @@ func (p *Parser) ParseMDDir(baseDirPath string, baseDirFS fs.FS) {
p.AddFile(baseDirPath, fileName, fronmatter, markdownContent, body)
}
}

} else {
helper.CopyFiles(helpers.SiteDataPath+"content/"+fileName, helpers.SiteDataPath+"rendered/"+fileName)
}
}
}
Expand All @@ -128,7 +125,6 @@ func (p *Parser) ParseMDDir(baseDirPath string, baseDirFS fs.FS) {

func (p *Parser) AddFile(baseDirPath string, dirEntryPath string, frontmatter Frontmatter, markdownContent string, body string) {
p.MdFilesName = append(p.MdFilesName, dirEntryPath)
// fmt.Println(baseDirPath, dirEntryPath)
filepath := baseDirPath + dirEntryPath
p.MdFilesPath = append(p.MdFilesPath, filepath)

Expand All @@ -155,7 +151,6 @@ func (p *Parser) AddFile(baseDirPath string, dirEntryPath string, frontmatter Fr

// Adding the page to the merged map storing all site pages
if frontmatter.Type == "post" {
// url = "posts/" + url
p.Posts = append(p.Posts, page)
}

Expand All @@ -167,12 +162,9 @@ func (p *Parser) AddFile(baseDirPath string, dirEntryPath string, frontmatter Fr
p.TagsMap[template.URL(tagsMapKey)] = append(p.TagsMap[template.URL(tagsMapKey)], page)

}

}

if frontmatter.Type == "note" {
// url = "notes/" + url

markdownContent = strings.TrimFunc(markdownContent, func(r rune) bool {
return r == '\n' || r == '\t'
})
Expand Down Expand Up @@ -220,6 +212,9 @@ func (p *Parser) ParseMarkdownContent(filecontent string) (Frontmatter, string,
return Frontmatter{}, "", "", false
}

// If the first section of the page contains a title field, continue parsing
// Else, prevent parsing of the current file
// TODO: Add this to documentation
regex := regexp.MustCompile(`title(.*): (.*)`)
match := regex.FindStringSubmatch(splitContents[1])

Expand Down
55 changes: 0 additions & 55 deletions site/content/posts/TODO.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ experience. It also motivates one to maintain and improve their personal site.

> Presented and written by Adhesh, Anirudh, Aditya and Nathan
Building personal blogs from the ground up can be a *tedious process*. Some of us
Building personal blogs from the ground up can be a _tedious process_. Some of us
have had our hands deep in vanilla HTML and CSS, which isn't fun to maintain. We
all want to get to the point and share our thoughts on the web. But, there is a
small bump that stops us from doing so.
Expand Down Expand Up @@ -116,17 +116,17 @@ Later, we implemented a front matter YAML parser to retrieve page metadata
## What made us develop this to a great extent?

- Beginner-friendly: An easy setup wizard, easy and ready to use layouts, and themes. We want the
process of typing out a blog and putting it up on your site to be short and
simple.
process of typing out a blog and putting it up on your site to be short and
simple.
- Speed: Be fast (hugo – written in Go, is remarkably fast)
- Maintainable: This ssg will be used by us, hence it should be easy to fix
bugs and add new features
bugs and add new features
- Learning curve: None of us have really shipped a production ready
application. Since AIEP is all about making industry-ready projects, we chose
to use go: so we could spend more ***writing** code* and not worrying about our
toolchain or escaping dependency hell.
application. Since AIEP is all about making industry-ready projects, we chose
to use go: so we could spend more **\*writing** code\* and not worrying about our
toolchain or escaping dependency hell.
- Owning a piece of the internet: Aditya and Anirudh registered their own
domain names. Now their anna sites live on [hegde.live] and [sudhir.live]
domain names. Now their anna sites live on [hegde.live] and [sudhir.live]

---

Expand All @@ -145,9 +145,9 @@ goroutines.
Here are some screenshots out of our group chats, that demonstrate build times, profiling et-al when having thousands of markdown files or in this case
just copy-pasting a single markdown file en-mass!

![Hyperfine benchmarks comparing the render times of anna, Saaru and 11ty](/static/images/posts/building-anna/bench.png)
![Hyperfine benchmarks comparing the render times of anna, Saaru and 11ty](images/bench.png)

> After about 2 weeks of training (*ahem*) coding, we had a (merge) bringing parallel rendering and profiling to the table
> After about 2 weeks of training (_ahem_) coding, we had a (merge) bringing parallel rendering and profiling to the table
---

Expand All @@ -161,6 +161,7 @@ Here,s the CPU profile generated while rendering this site.
This is an SVG showing how much time each function call takes, the number of times it ran in a given test sample and various other useful information.

![CPU profile of an anna render generated using pprof](https://raw.githubusercontent.com/acmpesuecc/anna/main/site/static/images/posts/building-anna/cpu_prof.svg)

<!-- ![CPU profile of an anna render generated using pprof](/static/images/posts/building-anna/cpu_prof.svg) -->

You may wanna zoom-in about 3-4x times to get to see how our ssg works
Expand Down Expand Up @@ -275,7 +276,7 @@ by category.

## To search or not to search? 🤔

> That is the question > Is our *static site* becoming dynamic and at what cost?
> That is the question > Is our _static site_ becoming dynamic and at what cost?
We were wondering if we’d need a search function on our site since Google and
any other web-crawler index our site anyway.
Expand Down Expand Up @@ -342,7 +343,7 @@ We are at week: 4/6 and have a lot of things in store and bugs to squash!
> Feel free to ask any questions / send feature requests you'd like to see?
This blog post misses out of many not-so well documented features and learnings that
we got during midnight calls and the patches we kept sending each other fixing trivial but
we got during midnight calls and the patches we kept sending each other fixing trivial but
interesting issues.
Have a look at our [GitHub](https://github.com/acmpesuecc/anna/issues), for more

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 66c653f

Please sign in to comment.