diff --git a/README.md b/README.md index dc28b61..220cdc5 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The language is very minimal and heavily inspired by Schemdraw's. This ships as a single binary, and is blazingly fast. -**NOT YET IMPLEMENTED:** Is easily included in mdbook as preprocessor. +[Is easily included in `mdbook` as a preprocessor!](https://schemgo.samake.se/mdbook) ## Quickstart @@ -36,7 +36,8 @@ go install github.com/sermuns/schemgo Create a file `simple.schemgo` containing: -```python + +```haskell battery.right line.up resistor.left @@ -75,17 +76,17 @@ The circuit diagram is created as `simple.svg`: - [x] `build` exports svg file - [x] `serve` serves a development website for live-preview - [x] Comments with `#` +- [x] mdBook preprocessor ### 🎯 High priority -- [x] Handle piped content (stdin, stdout) - - [ ] mdBook preprocessor +- [ ] Labels + - [ ] `typst` math +- [ ] multiple terminal: named anchors on components? ### Normal priority - [ ] Components (I won't attempt before situation with polymorphsim is under control.) - [ ] Optional style string on tags - [ ] (Better way of defining appearances... maybe external files?) - - [ ] multiple terminal: named anchors on components? - - [ ] More components - [x] Battery - [x] Resistor @@ -102,8 +103,6 @@ The circuit diagram is created as `simple.svg`: - [ ] GND (and 5V)? - [ ] `@set` statement to change global defaults (stroke width, padding, color) - [ ] set standard (IEC, IEEE) on elemnt, using `set` -- [ ] Labels - - [ ] `typst` math - [ ] Element attributes - [ ] ID for symbolic reference - [ ] Exporting to pdf, png, jpg, webp diff --git a/cmd/mdbook.go b/cmd/mdbook.go index 6618229..676fe4f 100644 --- a/cmd/mdbook.go +++ b/cmd/mdbook.go @@ -1,35 +1,96 @@ package cmd import ( - // "encoding/json" + "bufio" + "encoding/json" "fmt" - "github.com/spf13/cobra" "io" + "log" "os" + "strings" + + "github.com/spf13/cobra" ) +type Chapter struct { + Name string `json:"name"` + Content string `json:"content"` + Number []int `json:"number"` + SubItems []string `json:"sub_items"` + Path string `json:"path"` + SourcePath string `json:"source_path"` + ParentNames []string `json:"parent_names"` +} + +type Section struct { + Chapter Chapter `json:"Chapter"` +} + +type Config struct { + Sections []Section `json:"sections"` + NonExhaustive *string `json:"__non_exhaustive"` +} + +// find ```schemgo ``` md code blocks, run them through the schemgo processor. +// replace entire block (including ticks) with the output of processor +func processSection(section *Section) { + var processed strings.Builder + scanner := bufio.NewScanner(strings.NewReader(section.Chapter.Content)) + var schemgoContent []byte + + for scanner.Scan() { + line := scanner.Text() + if !strings.HasPrefix(line, "```schemgo") { + processed.WriteString(line + "\n") + continue + } + + schemgoContent = []byte{} + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "```") { + break + } + schemgoContent = append(schemgoContent, []byte(line+"\n")...) + } + processed.Write(writeSchematic(schemgoContent)) + processed.WriteString("\n") + } + + section.Chapter.Content = processed.String() +} + var mdbookCmd = &cobra.Command{ Use: "mdbook", Short: "Act as mdBook preprocessor. You probably don't want to manually use this!", Run: func(cmd *cobra.Command, args []string) { - // mdbook wants this - if len(args) > 1 && args[1] == "supports" { + if len(args) > 1 && args[0] == "supports" { os.Exit(0) } - content, err := io.ReadAll(os.Stdin) + stdInput, err := io.ReadAll(cmd.InOrStdin()) if err != nil { - fmt.Println("Error reading from stdin:", err) + fmt.Fprintf(os.Stderr, "Failed to read stdin: %v\n", err) os.Exit(1) } - // var data map[string]interface{} + var jsonData []Config + err = json.Unmarshal(stdInput, &jsonData) + if err != nil { + log.Fatal(err) + } + + book := jsonData[1] + for i := range book.Sections { + processSection(&book.Sections[i]) + } + out, err := json.Marshal(book) if err != nil { - fmt.Println("heh", content) - fmt.Println("Error parsing JSON:", err) - os.Exit(1) + log.Fatal(err) } + + fmt.Print(string(out)) }, } diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index e4b271a..45eec8b 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -6,6 +6,7 @@ nav: - Installation: installation.md - Tutorial: - Getting started: getting-started.md + - "mdBook preprocessor": mdbook.md - Reference: - Components: components.md - Examples: examples.md diff --git a/docs/src/mdbook.md b/docs/src/mdbook.md new file mode 100644 index 0000000..fdb7860 --- /dev/null +++ b/docs/src/mdbook.md @@ -0,0 +1,31 @@ +--- +hide: + - toc +--- + +# `mdBook` preprocessor + +The original idea for `schemgo` was conceived as a simple `mdBook` preprocessor! + +You can write `schemgo` directly in your book by adding `schemgo` as a preprocessor. + +~~~markdown +## Subheading + +This just markdown. Talking about the schematic... + +```schemgo +battery.right +line.up +dot +``` +~~~ + +## Setup + +Add `schemgo` as a preprocessor by adding this line to your configuration: + +```toml +[preprocessor.schemgo] +command = "schemgo mdbook" +``` diff --git a/docs/src/style.css b/docs/src/style.css index 5b0eded..34ab1c8 100644 --- a/docs/src/style.css +++ b/docs/src/style.css @@ -3,10 +3,6 @@ p > img { margin: auto; } -body[data-md-color-scheme="slate"] - main - img[src$=".svg"] - :not([src$="banner.svg"]) - :not([src$="logo.svg"]) { +body[data-md-color-scheme="slate"] main img[src$=".svg"]:not([src$="banner.svg"]):not([src$="logo.svg"]) { filter: invert(1); } diff --git a/media/newlogo.svg b/media/newlogo.svg new file mode 100644 index 0000000..93ee5ad --- /dev/null +++ b/media/newlogo.svg @@ -0,0 +1,549 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +