utils

package
v0.0.0-...-527be62 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: BSD-3-Clause Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BODY          = "__BODY__"
	HEADER        = "__HEADER__"
	INDEX         = "__INDEX__"
	CSS           = "__CSS__"
	URL           = "__URL__"
	IMAGE_DIR     = "images"
	CSS_FILE_NAME = "app.css"
)
View Source
const (
	DateTime      = "2006-01-02 15:04"
	MAX_RSS_ITEMS = 20
	RSS_FILE_NAME = "rss.xml"
	ITEM_TEMPLATE = `` /* 126-byte string literal not displayed */

	RSS_TEMPLATE = `` /* 311-byte string literal not displayed */

)

Variables

View Source
var (
	TITLE       = regexp.MustCompile("__TITLE__")
	DESCRIPTION = regexp.MustCompile("__DESCRIPTION__")
	SEPARATOR   = regexp.MustCompile(`---(.*)`)
)

Functions

func CopyDir

func CopyDir(src, dist string) error

CopyDir copies a directory from src to dist. It supports both Windows and Unix-like operating systems. On Windows, it uses robocopy, and on Unix-like systems, it uses cp. It returns an error with a stack trace

func CreateCategoryOrders

func CreateCategoryOrders(categories string) map[string]int

CreateCategoryOrders takes a string of categories separated by commas, trims any trailing comma, and then splits the string into individual categories. It creates and returns a map where each category is a key with its value being the order (index) in which the category appears in the input string. If a category appears more than once, it is only added to the map once, with the index of its first occurrence.

Parameters: - categories: A string of categories separated by commas. For example, "apple,orange,banana,apple".

Returns:

  • A map[string]int where keys are unique categories from the input string and values are the indexes at which those categories first appear in the input string.

Note: This function does not account for spaces around category names. For example, "apple, orange" will treat " orange" (with a leading space) as a distinct category from "orange".

func CreateDescription

func CreateDescription(htmlStr string) (string, error)

CreateDescription generates a description from an HTML string. It extracts text nodes and removes newlines and specific characters, then uses the first 300 characters to generate the description.

func CreateHTML

func CreateHTML(layout, title, body, description, url, cssPath, indexMenu, headerList string) string

CreateHTML generates an HTML page using the given parameters. It applies the parameters to the layout template and returns the completed HTML string.

func CreateHTMLFileDir

func CreateHTMLFileDir(dir, sourceDir, outputDir string) string

CreateHTMLFileDir constructs the file path for an HTML file based on the given directory, source directory, and output directory. It calculates the subdirectory path by removing the source directory prefix from the given directory path. This subdirectory path is then appended to the output directory to form the final file path. Parameters: - dir: The directory where the original Markdown file resides. It is expected to be a subdirectory of the source directory. - sourceDir: The root directory of all source Markdown files. - outputDir: The root directory where the resulting HTML files should be saved. Returns: The constructed file path for the HTML file, which combines the output directory and the subdirectory derived from the given directory, excluding the source directory prefix.

func CreateHash

func CreateHash(text string) string

CreateHash generates a hash value from the given text. It replaces specific characters with "_", "-_", and "_-" to create the hash.

func CreateHeaderList

func CreateHeaderList(md string) (string, error)

CreateHeaderList generates HTML for a header list from markdown text.

func CreateIndexMenu

func CreateIndexMenu(items []IndexItem) string

CreateIndexMenu generates an HTML navigation menu from a slice of IndexItem.

func CreateIndexPage

func CreateIndexPage(layout, baseURL, header, title, description, cssPath string, indexItems []IndexItem) (string, error)

CreateIndexPage generates the HTML for an index page from index items.

func CreatePage

func CreatePage(layout, md, title, url, cssPath, indexMenu, headerList string) (string, error)

CreatePage generates the HTML for an individual page using the given parameters.

func CreateRssFileTask

func CreateRssFileTask(pages []*Page, tz, outputDir, baseURL, title, description string) func() error

func CreateTitle

func CreateTitle(md string) string

CreateTitle generates a title from the markdown text. It uses the first heading (# Title) as the title.

func CreateURL

func CreateURL(dir, name, sourceDir, baseURL string) string

CreateURL generates a URL from the specified directory and file name. It constructs the URL based on the source directory and base URL obtained from environment variables.

func Filter

func Filter[T any](items []T, callbackFn func(item T, index int) (bool, error)) ([]T, error)

Filter applies a callback function to each item in a slice and returns a new slice containing only the items for which the callback returns true. The callback function receives an item and its index as arguments and returns a boolean and an error. If the callback function returns an error, the filtering process stops and the error is returned.

func GetDirAndName

func GetDirAndName(path string) (string, string)

GetDirAndName extracts the directory path and file name (without extension) from a file path.

func GetMarkDownFileNames

func GetMarkDownFileNames(fp IFilePath, root string) ([]string, error)

GetMarkDownFileNames searches the specified root directory and all of its subdirectories for files with the ".md" extension and returns a slice containing the paths of all markdown files found. Parameters: - root: The root directory from which the search will begin.

func GetMd

func GetMd(content string) (string, error)

func ImageSize

func ImageSize(path string) (int, int, error)

ImageSize takes the path to an image file and returns the dimensions of the image. Supported image formats include JPEG, PNG, GIF, BMP, TIFF, and WEBP, thanks to the standard image package and additional formats provided by the golang.org/x/image package.

Parameters: - path: A string representing the filesystem path to the image file.

Returns: - The width (Dx) and height (Dy) of the image as integers. - An error if the file cannot be opened, or if the image format is not recognized or is corrupt.

func IsCode

func IsCode(line string) bool

IsHeader determines if a given text line is a markdown code.

func IsDirExists

func IsDirExists(path string) bool

IsDirExists checks if the directory at the given path exists. It returns true if the directory exists, and false otherwise.

func IsHeader

func IsHeader(line string) bool

IsHeader determines if a given text line is a markdown header.

func Keys

func Keys[K comparable, V any](items map[K]V) []K

Keys extracts the keys from a map and returns them as a slice. The order of the keys in the returned slice is not specified and may vary.

func Map

func Map[T any, R any](items []T, callbackFn func(item T, index int) (R, error)) ([]R, error)

Map applies a callback function to each item in a slice and returns a new slice of the results. The callback function receives an item and its index as arguments and returns a new value and an error. If the callback function returns an error, the mapping process stops and the error is returned.

func MapToSlice

func MapToSlice[V any](m map[int]V) ([]V, error)

MapToSlice converts a map with integer keys to a slice. It sorts the keys and populates the slice with values in that order.

func NewMarkdown

func NewMarkdown() goldmark.Markdown

NewMarkdown initializes a new goldmark.Markdown instance with custom rendering logic. It includes GitHub Flavored Markdown (GFM) extensions and sets the custom renderer with high priority.

Types

type Category

type Category struct {
	Name  string `json:"name,omitempty"`
	Order int    `json:"order,omitempty"`
}

type FilePath

type FilePath struct{}

func (FilePath) WalkDir

func (fp FilePath) WalkDir(root string, fn fs.WalkDirFunc) error

type IFilePath

type IFilePath interface {
	WalkDir(root string, fn fs.WalkDirFunc) error
}

type IndexItem

type IndexItem struct {
	Name  string          `json:"name,omitempty"`
	Pages []IndexItemPage `json:"pages,omitempty"`
}

func CreateIndexItems

func CreateIndexItems(pages []*Page) ([]IndexItem, error)

CreateIndexItems generates a slice of IndexItem from a slice of Pages. It organizes pages by header order and creates IndexItem structs containing each header and page titles and URLs.

type IndexItemPage

type IndexItemPage struct {
	Title string `json:"title,omitempty"`
	URL   string `json:"url,omitempty"`
}

type IndexItemPagesMap

type IndexItemPagesMap map[int]IndexItemPage

type Meta

type Meta struct {
	Category Category `json:"header,omitempty"`
	Order    int      `json:"order,omitempty"`
	Date     string   `json:"date,omitempty"`
}

func GetMetaAndMd

func GetMetaAndMd(content string, categoryOrders map[string]int) (*Meta, string, error)

GetMetaAndMd extracts metadata and markdown text from the content of a markdown file. The content is split by "---", where the first part is interpreted as JSON format metadata, and the second part as markdown text.

type Page

type Page struct {
	Meta  Meta   `json:"meta,omitempty"`
	Title string `json:"title,omitempty"`
	URL   string `json:"url,omitempty"`
}

func CreatePageData

func CreatePageData(markDownFileName, sourceDir, baseURL string, categoryOrders map[string]int) (*Page, error)

CreatePageData generates page data from a markdown file name. It parses the file content to extract metadata, title, and URL, and returns a Page struct containing these.

func CreatePages

func CreatePages(markDownFileNames []string, sourceDir, baseURL, categories string) ([]*Page, error)

CreatePages asynchronously generates a slice of Page data from multiple markdown files.

type PageMeta

type PageMeta struct {
	Category string `json:"category,omitempty"`
	Order    int    `json:"order,omitempty"`
	Date     string `json:"date,omitempty"`
}

type SortedLimitedArray

type SortedLimitedArray[T any] struct {
	Items     []T               // Items holds the elements of the array.
	Limit     int               // Limit specifies the maximum number of elements the array can hold.
	CompareFn func(a, b T) bool // CompareFn is the function used to compare elements in the array.
}

SortedLimitedArray represents a slice that maintains a limited number of elements in a sorted order. The slice is constrained to the specified limit and sorted based on the provided comparison function.

func NewSortedLimitedArray

func NewSortedLimitedArray[T any](items []T, limit int, compareFn func(a, b T) bool) *SortedLimitedArray[T]

NewSortedLimitedArray creates and returns a new SortedLimitedArray with the specified limit and comparison function.

func (*SortedLimitedArray[T]) Push

func (s *SortedLimitedArray[T]) Push(items ...T)

Push adds elements to the array and then sorts and truncates it to ensure it doesn't exceed the specified limit. The elements are added to the array, and then the array is sorted and truncated according to the CompareFn and Limit.

Directories

Path Synopsis
Package mock_utils is a generated GoMock package.
Package mock_utils is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL