~humaid/godoc2markdown

ceb45a75cb678a3e6606eff712413584ad5f2850 — Humaid AlQassimi 5 months ago v0.1
Initial commit
6 files changed, 112 insertions(+), 0 deletions(-)

A .build.yml
A .gitignore
A LICENSE
A Makefile
A go.mod
A main.go
A  => .build.yml +19 -0
@@ 1,19 @@
image: archlinux
packages:
  - go
sources:
  - https://git.sr.ht/~humaid/godoc2markdown
tasks:
  - setup: |
      cd godoc2markdown
      go get ./...
  - format: |
      cd godoc2markdown
      go fmt $(go list ./... | grep -v /vendor/)
      go vet $(go list ./... | grep -v /vendor/)
  - test: |
      cd godoc2markdown
      go test -cover -race $(go list ./... | grep -v /vendor/)
  - build: |
      cd godoc2markdown
      go build

A  => .gitignore +2 -0
@@ 1,2 @@
*.txt
godoc2markdown

A  => LICENSE +26 -0
@@ 1,26 @@
BSD 2-Clause License

Copyright (c) 2019, Humaid AlQassimi
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


A  => Makefile +14 -0
@@ 1,14 @@
GC = go

TARGET = godoc2markdown

all: $(TARGET)

clean:
	$(RM) $(TARGET)

$(TARGET): main.go
	$(GC) build

install:
	$(GC) install

A  => go.mod +3 -0
@@ 1,3 @@
module git.sr.ht/~humaid/godoc2markdown

go 1.13

A  => main.go +48 -0
@@ 1,48 @@
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strings"
)

var (
	titleExp = regexp.MustCompile("^[A-Z]+")
	funcExp  = regexp.MustCompile("^func ")
	descExp  = regexp.MustCompile("^    ")
)

func main() {
	dat, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}

	lines := strings.Split(string(dat), "\n")
	codeBlock := ""

	for _, line := range lines {
		if len(line) == 0 {
			if len(codeBlock) == 0 {
				fmt.Println()
			} else {
				fmt.Printf("```go%s\n```\n", codeBlock)
				codeBlock = ""
			}
		} else {
			if titleExp.Match([]byte(line)) {
				fmt.Printf("## %s\n", line)
			} else if descExp.Match([]byte(line)) {

				fmt.Printf("%s\n", descExp.ReplaceAll([]byte(line), []byte("")))
			} else if funcExp.Match([]byte(line)) {
				name := strings.Split(strings.Split(line, " ")[1], "(")[0]
				fmt.Printf("## func %s\n```go\n%s\n```\n", name, line)
			} else {
				codeBlock = fmt.Sprintf("%s\n%s", codeBlock, line)
			}
		}
	}
}