initial commit
This commit is contained in:
commit
7f49aac0cc
72
main.go
Normal file
72
main.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
// TODO: read a file line by line
|
||||
// use heading struct with
|
||||
// line content
|
||||
// line number
|
||||
// heading level
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func is_heading(line string) bool {
|
||||
if strings.HasPrefix(line, "#") {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func get_heading_level(heading string) int {
|
||||
level := 0
|
||||
for i := 0; heading[i] == '#'; i++ {
|
||||
level += 1
|
||||
}
|
||||
return level
|
||||
}
|
||||
|
||||
func print_toc(headings []string) {
|
||||
tab_count := 0
|
||||
for index, value := range headings {
|
||||
if index > 0 {
|
||||
prev_level := get_heading_level(headings[index-1])
|
||||
cur_level := get_heading_level(headings[index])
|
||||
|
||||
if cur_level > prev_level {
|
||||
tab_count += (cur_level - prev_level)
|
||||
} else if cur_level < prev_level {
|
||||
tab_count -= (prev_level - cur_level)
|
||||
}
|
||||
for i := 0; i < tab_count; i++ {
|
||||
fmt.Printf("\t")
|
||||
}
|
||||
}
|
||||
fmt.Printf("%s\n", value)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
file_content_raw, err := os.ReadFile(os.Args[1])
|
||||
|
||||
var headings []string = nil
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var file_content string = string(file_content_raw)
|
||||
|
||||
file_lines := strings.Split(file_content, "\n")
|
||||
|
||||
for _, value := range file_lines {
|
||||
if is_heading(value) {
|
||||
headings = append(headings, value)
|
||||
}
|
||||
}
|
||||
|
||||
print_toc(headings)
|
||||
}
|
31
test.md
Normal file
31
test.md
Normal file
@ -0,0 +1,31 @@
|
||||
# test markdown doc
|
||||
|
||||
## header2
|
||||
|
||||
#### header4
|
||||
|
||||
some text.
|
||||
|
||||
```bash
|
||||
echo 'hi'
|
||||
```
|
||||
|
||||
#### another header4
|
||||
|
||||
more text.
|
||||
|
||||
####third header4
|
||||
|
||||
####fourth #header4
|
||||
|
||||
## this is not a header anymore
|
||||
|
||||
##### header5
|
||||
|
||||
## a second header2
|
||||
|
||||
#### testing
|
||||
|
||||
```
|
||||
cool code
|
||||
```
|
57
tests/README.md
Normal file
57
tests/README.md
Normal file
@ -0,0 +1,57 @@
|
||||
# yamlfmt
|
||||
|
||||
`yamlfmt` is an extensible command line tool or library to format yaml files.
|
||||
|
||||
## Goals
|
||||
|
||||
* Create a command line yaml formatting tool that is easy to distribute (single binary)
|
||||
* Make it simple to extend with new custom formatters
|
||||
* Enable alternative use as a library, providing a foundation for users to create a tool that meets specific needs
|
||||
|
||||
## Maintainers
|
||||
|
||||
This tool is not yet officially supported by Google. It is currently maintained solely by @braydonk, and unless something changes primarily in spare time.
|
||||
|
||||
## Blog
|
||||
|
||||
I'm going to use these links to GitHub Discussions as a blog of sorts, until I can set up something more proper:
|
||||
* yamlfmt's recent slow development [#149](https://github.com/google/yamlfmt/discussions/149)
|
||||
* Issues related to the yaml.v3 library [#148](https://github.com/google/yamlfmt/discussions/148)
|
||||
|
||||
## Installation
|
||||
|
||||
To download the `yamlfmt` command, you can download the desired binary from releases or install the module directly:
|
||||
```
|
||||
go install github.com/google/yamlfmt/cmd/yamlfmt@latest
|
||||
```
|
||||
This currently requires Go version 1.18 or greater.
|
||||
|
||||
NOTE: Recommended setup if this is your first time installing Go would be in [this DigitalOcean blog post](https://www.digitalocean.com/community/tutorials/how-to-build-and-install-go-programs).
|
||||
|
||||
You can also download the binary you want from releases. The binary is self-sufficient with no dependencies, and can simply be put somewhere on your PATH and run with the command `yamlfmt`.
|
||||
|
||||
You can also install the command as a [pre-commit](https://pre-commit.com/) hook. See the [pre-commit hook](./docs/pre-commit.md) docs for instructions.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
See [Command Usage](./docs/command-usage.md) for in-depth information and available flags.
|
||||
|
||||
To run the tool with all default settings, run the command with a path argument:
|
||||
```bash
|
||||
yamlfmt x.yaml y.yaml <...>
|
||||
```
|
||||
You can specify as many paths as you want. You can also specify a directory which will be searched recursively for any files with the extension `.yaml` or `.yml`.
|
||||
```bash
|
||||
yamlfmt .
|
||||
```
|
||||
|
||||
You can also use an alternate mode that will search paths with doublestar globs by supplying the `-dstar` flag.
|
||||
```bash
|
||||
yamlfmt -dstar **/*.{yaml,yml}
|
||||
```
|
||||
See the [doublestar](https://github.com/bmatcuk/doublestar) package for more information on this format.
|
||||
|
||||
# Configuration File
|
||||
|
||||
The `yamlfmt` command can be configured through a yaml file called `.yamlfmt`. This file can live in your working directory, a path specified through a [CLI flag](./docs/command-usage.md#operation-flags), or in the standard global config path on your system (see docs for specifics).
|
||||
For in-depth configuration documentation see [Config](docs/config-file.md).
|
Loading…
Reference in New Issue
Block a user