Skip to content

Latest commit

 

History

History
257 lines (205 loc) · 4.85 KB

File metadata and controls

257 lines (205 loc) · 4.85 KB

Code Block Showcase

Simple Code Blocks

Python

def fibonacci(n):
    """Generate Fibonacci sequence."""
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

print(list(fibonacci(10)))

JavaScript

const fetchData = async (url) => {
  const response = await fetch(url);
  const data = await response.json();
  return data;
};

// Usage
fetchData('https://api.example.com/posts')
  .then(posts => console.log(posts));

Shell

#!/bin/bash
for file in *.org; do
    echo "Processing: $file"
    wc -l "$file"
done

SQL

SELECT
    p.title,
    p.status,
    COUNT(c.id) as comment_count
FROM posts p
LEFT JOIN comments c ON c.post_id = p.id
WHERE p.published_at > '2026-01-01'
GROUP BY p.id
ORDER BY comment_count DESC
LIMIT 10;

Emacs Lisp

(defun org-to-ghost (file)
  "Export FILE to Ghost Lexical JSON."
  (interactive "fOrg file: ")
  (with-temp-buffer
    (insert-file-contents file)
    (org-mode)
    (org-export-as 'lexical)))

Code with Output

Python with text output

import sys
print(f"Python {sys.version}")
print("Hello from Python!")
Python 3.12.0 (main, Oct  2 2024)
Hello from Python!

Shell with output

echo "Current date: $(date)"
echo "User: $(whoami)"
Current date: Wed Jan 29 11:45:00 EST 2026
User: hh

Code Generating Code

Python generating JSON

import json

config = {
    "site": "ii.coop",
    "theme": "casper",
    "features": ["members", "newsletters", "comments"],
    "settings": {
        "posts_per_page": 10,
        "navigation": [
            {"label": "Home", "url": "/"},
            {"label": "About", "url": "/about/"}
        ]
    }
}

print(json.dumps(config, indent=2))
{
  "site": "ii.coop",
  "theme": "casper",
  "features": ["members", "newsletters", "comments"],
  "settings": {
    "posts_per_page": 10,
    "navigation": [
      {"label": "Home", "url": "/"},
      {"label": "About", "url": "/about/"}
    ]
  }
}

Python generating SQL

tables = ['users', 'posts', 'comments']
for table in tables:
    print(f"SELECT * FROM {table} LIMIT 5;")
SELECT * FROM users LIMIT 5;
SELECT * FROM posts LIMIT 5;
SELECT * FROM comments LIMIT 5;

Elisp generating YAML

"#+BEGIN_SRC yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: ghost-config
data:
  url: https://ii.coop
  mail__transport: SMTP
apiVersion: v1
kind: ConfigMap
metadata:
  name: ghost-config
data:
  url: https://ii.coop
  mail__transport: SMTP

Multi-language Pipeline

Step 1: Generate data (Python)

# Simulate fetching data
data = [
    {"id": 1, "name": "Alice", "role": "admin"},
    {"id": 2, "name": "Bob", "role": "editor"},
    {"id": 3, "name": "Carol", "role": "author"}
]
import json
print(json.dumps(data))
[{"id": 1, "name": "Alice", "role": "admin"}, {"id": 2, "name": "Bob", "role": "editor"}, {"id": 3, "name": "Carol", "role": "author"}]

Step 2: Transform with jq (Shell)

echo '[{"id": 1, "name": "Alice", "role": "admin"}, {"id": 2, "name": "Bob", "role": "editor"}]' | \
  jq '.[] | "User: \(.name) (\(.role))"'
"User: Alice (admin)"
"User: Bob (editor)"

Step 3: Generate config (YAML output)

import yaml

config = {
    'users': [
        {'name': 'Alice', 'permissions': ['read', 'write', 'admin']},
        {'name': 'Bob', 'permissions': ['read', 'write']}
    ]
}

print(yaml.dump(config, default_flow_style=False))
users:
- name: Alice
  permissions:
  - read
  - write
  - admin
- name: Bob
  permissions:
  - read
  - write

Inline Examples

Here’s how you might use kubectl to check pods:

kubectl get pods -n ghost

The output would look like:

NAME                     READY   STATUS    RESTARTS   AGE
ghost-6d4f5b7c8-abc12    1/1     Running   0          2d
ghost-6d4f5b7c8-def34    1/1     Running   0          2d
mysql-7f8d9c6b5-xyz99    1/1     Running   0          5d

Summary

This document demonstrates:

  1. Simple code blocks in multiple languages
  2. Code with text output (:exports both)
  3. Code generating other code formats (JSON, SQL, YAML)
  4. Multi-step pipelines with different languages
  5. Example blocks for sample output