Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Extract code snippets

on:
push:
paths:
- "fabric/examples/**"
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

jobs:
extract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Extract snippets
run: |
cd fabric/examples
mkdir -p /tmp/snippets

for lang_file in \
rust/src/main.rs \
js/index.mjs \
go/main.go \
python/example.py \
kotlin/Example.kt \
swift/Example.swift; do

lang=$(dirname "$lang_file" | cut -d/ -f1)
mkdir -p "/tmp/snippets/$lang"
./extract-snippets.sh "$lang_file" "/tmp/snippets/$lang"
done

# Create index
echo "# Code Snippets" > /tmp/snippets/index.md
echo "" >> /tmp/snippets/index.md
for lang in rust js go python kotlin swift; do
echo "## $lang" >> /tmp/snippets/index.md
for f in /tmp/snippets/$lang/*.txt; do
name=$(basename "$f" .txt)
echo "- [$name]($lang/$name.txt)" >> /tmp/snippets/index.md
done
echo "" >> /tmp/snippets/index.md
done

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: /tmp/snippets

deploy:
needs: extract
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions fabric/examples/extract-snippets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
set -eo pipefail
#
# Extract code snippets from example files.
#
# Snippets are delimited by <doc:name> and </doc:name> tags.
# Output: one file per snippet in the output directory.
#
# Usage:
# ./extract-snippets.sh rust/src/main.rs snippets/rust
# ./extract-snippets.sh js/index.mjs snippets/js
#

INPUT="${1:?Usage: extract-snippets.sh <input-file> <output-dir>}"
OUTDIR="${2:?Usage: extract-snippets.sh <input-file> <output-dir>}"

mkdir -p "$OUTDIR"

# Detect language from file extension
EXT="txt"

# Extract each <doc:name> ... </doc:name> block
current_tag=""
current_file=""

while IFS= read -r line; do
# Check for opening tag
if [[ "$line" =~ \<doc:([a-zA-Z0-9_-]+)\> ]]; then
current_tag="${BASH_REMATCH[1]}"
current_file="$OUTDIR/${current_tag}.${EXT}"
> "$current_file" # truncate
continue
fi

# Check for closing tag
if [[ "$line" =~ \</doc: ]]; then
if [ -n "$current_file" ]; then
echo " extracted: $current_tag -> $current_file"
fi
current_tag=""
current_file=""
continue
fi

# Write line to current snippet (strip common leading whitespace)
if [ -n "$current_file" ]; then
echo "$line" >> "$current_file"
fi
done < "$INPUT"

# Clean up: strip common leading whitespace from each snippet
for f in "$OUTDIR"/*."$EXT"; do
[ -f "$f" ] || continue
# Find minimum indentation (ignoring blank lines)
min_indent=$(sed -n '/[^ ]/{ s/^\( *\).*/\1/; p; }' "$f" | awk '{ print length }' | sort -n | head -1)
if [ -n "$min_indent" ] && [ "$min_indent" -gt 0 ]; then
if sed -i '' "s/^ \{1,$min_indent\}//" "$f" 2>/dev/null; then
: # macOS sed
else
sed -i "s/^ \{1,$min_indent\}//" "$f" # Linux sed
fi
fi
done

echo "Done: $(ls "$OUTDIR"/*."$EXT" 2>/dev/null | wc -l | tr -d ' ') snippets extracted"
13 changes: 13 additions & 0 deletions fabric/examples/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module example-go

go 1.22

require github.com/spacesprotocol/fabric-go v0.0.0-dev.20260401232107

require (
github.com/spacesprotocol/libveritas-go v0.0.0-dev.20260401232107 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.6 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
)
Loading
Loading