<commit_before>package blog

import (
	"github.com/crockeo/personalwebsite/config"
	"io/ioutil"
	"strconv"
)

// Loading a Post from a file
func LoadPostRaw(path string) (*Post, error) {
	val, err := ioutil.ReadFile(path)

	if err != nil {
		return nil, err
	}

	return ParsePost(string(val)), nil
}

// Loading a Post from index
func LoadPost(index int) (*Post, error) {
	return LoadPostRaw(config.PostsDir + config.PostName + strconv.FormatInt(int64(index), 10))
}

// Loading the nubmer of Posts
func Posts() int {
	val, err := ioutil.ReadFile(config.PostIndexLoc)

	if err != nil {
		return 0
	}

	ret, err := strconv.ParseInt(string(val), 10, 64)

	if err != nil {
		return 0
	}

	return int(ret)
}

// Writing the number of Posts
func SetPosts(num int) error {
	return ioutil.WriteFile(config.PostIndexLoc, []byte(strconv.FormatInt(int64(num), 10)), 664)
}

// Incrementing the nubmer of Posts that exist
func IncPosts() error {
	return SetPosts(Posts() + 1)
}

// Loading every post
func LoadPosts() ([]*Post, error) {
	nposts := Posts()

	if nposts == 0 {
		return nil, nil
	} else {
		posts := make([]*Post, nposts)
		for i := 0; i < nposts; i++ {
			post, err := LoadPost(i)

			if err != nil {
				return nil, err
			}<commit_msg>Added utils for saving posts (untested)<commit_after>package blog

import (
	"github.com/crockeo/personalwebsite/config"
	"io/ioutil"
	"strconv"
)

// Loading a Post from a file
func LoadPostRaw(path string) (*Post, error) {
	val, err := ioutil.ReadFile(path)

	if err != nil {
		return nil, err
	}

	return ParsePost(string(val)), nil
}

// Writing a Post to a file
func SavePostRaw(path string, post *Post) error {
	return ioutil.WriteFile(path, []byte(post.String()), 664)
}

// Loading a Post from index
func LoadPost(index int) (*Post, error) {
	return LoadPostRaw(config.PostsDir + config.PostName + strconv.FormatInt(int64(index), 10))
}

// Saving a Post to an index
func SavePost(index int, post *Post) error {
	return SavePostRaw(config.PostsDir+config.PostName+strconv.FormatInt(int64(index), 10), post)
}

// Loading the nubmer of Posts
func Posts() int {
	val, err := ioutil.ReadFile(config.PostIndexLoc)

	if err != nil {
		return 0
	}

	ret, err := strconv.ParseInt(string(val), 10, 64)

	if err != nil {
		return 0
	}

	return int(ret)
}

// Saving a Post to the next available index
func SavePostNext(post *Post) error {
	posts := Posts()
	err := SavePost(posts+1, post)
	if err != nil {
		return err
	}

	return SetPosts(posts + 1)
}

// Writing the number of Posts
func SetPosts(num int) error {
	return ioutil.WriteFile(config.PostIndexLoc, []byte(strconv.FormatInt(int64(num), 10)), 664)
}

// Incrementing the nubmer of Posts that exist
func IncPosts() error {
	return SetPosts(Posts() + 1)
}

// Loading every post
func LoadPosts() ([]*Post, error) {
	nposts := Posts()

	if nposts == 0 {
		return nil, nil
	} else {
		posts := make([]*Post, nposts)
		for i := 0; i < nposts; i++ {
			post, err := LoadPost(i)

			if err != nil {
				return nil, err
			}
{'fraction_non_alphanumeric': 0.13949635520212061, 'fraction_numerical': 0.016235917826375082, 'mean_word_length': 3.7618296529968456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6554394', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1080, 'n_words': 328}