Skip to content

Commit

Permalink
Implement mdbook preprocessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
sermuns committed Jan 27, 2025
2 parents 74a8da7 + e2fa098 commit 5a9dc49
Show file tree
Hide file tree
Showing 6 changed files with 660 additions and 23 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -36,7 +36,8 @@ go install github.com/sermuns/schemgo

Create a file `simple.schemgo` containing:

```python
<!-- abusing linguist syntax highlighting.. this is NOT haskell -->
```haskell
battery.right
line.up
resistor.left
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
81 changes: 71 additions & 10 deletions cmd/mdbook.go
Original file line number Diff line number Diff line change
@@ -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))
},
}

Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions docs/src/mdbook.md
Original file line number Diff line number Diff line change
@@ -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"
```
6 changes: 1 addition & 5 deletions docs/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit 5a9dc49

Please sign in to comment.