Skip to content

Commit a6bf5a6

Browse files
committed
Add live example book
1 parent 6abcd19 commit a6bf5a6

File tree

7 files changed

+364
-3
lines changed

7 files changed

+364
-3
lines changed

.github/workflows/pages.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
9+
10+
jobs:
11+
deploy:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
concurrency:
18+
group: "pages"
19+
cancel-in-progress: false
20+
environment:
21+
name: github-pages
22+
url: ${{ steps.deployment.outputs.page_url }}
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
- name: Install Rust
27+
uses: actions-rs/toolchain@v1
28+
with:
29+
profile: minimal
30+
toolchain: stable
31+
override: true
32+
33+
- name: Install mdBook
34+
uses: peaceiris/actions-mdbook@v1
35+
with:
36+
mdbook-version: 'latest'
37+
38+
- name: Install mdbook-langtabs
39+
run: |
40+
cargo install --path .
41+
42+
- name: Setup Pages
43+
id: pages
44+
uses: actions/configure-pages@v3
45+
46+
- name: Build example book
47+
run: |
48+
cd example-book
49+
mdbook-langtabs install .
50+
mdbook build
51+
52+
- name: Upload artifact
53+
uses: actions/upload-pages-artifact@v1
54+
with:
55+
path: ./example-book/book
56+
57+
- name: Deploy to GitHub Pages
58+
id: deployment
59+
uses: actions/deploy-pages@v2
60+
if: github.ref == 'refs/heads/main'

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/target
2-
/test-book
1+
/target

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ An [mdBook](https://github.com/rust-lang/mdBook) preprocessor that creates langu
77

88
![Language tabs example demo](https://raw.githubusercontent.com/nx10/mdbook-langtabs/refs/heads/main/demo.gif)
99

10+
Also check out the [live demo](https://nx10.github.io/mdbook-langtabs/).
11+
1012
## Features
1113

1214
- Switch between language examples without scrolling
1315
- Automatic language icons via [Devicon](https://devicon.dev/)
14-
- Remembers selected language preference
1516

1617
## Installation
1718

example-book/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

example-book/book.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[book]
2+
authors = ["Your Name"]
3+
language = "en"
4+
multilingual = false
5+
src = "src"
6+
title = "Example Book"

example-book/src/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Summary
2+
3+
- [Examples](./examples.md)

example-book/src/examples.md

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
# Examples
2+
3+
## Hello World
4+
5+
Here's a simple "Hello World" example in different languages:
6+
7+
<!-- langtabs-start -->
8+
```rust
9+
fn main() {
10+
println!("Hello, World!");
11+
}
12+
```
13+
14+
```python
15+
def main():
16+
print("Hello, World!")
17+
18+
if __name__ == "__main__":
19+
main()
20+
```
21+
22+
```javascript
23+
function main() {
24+
console.log("Hello, World!");
25+
}
26+
27+
main();
28+
```
29+
30+
```go
31+
package main
32+
33+
import "fmt"
34+
35+
func main() {
36+
fmt.Println("Hello, World!")
37+
}
38+
```
39+
40+
```java
41+
public class HelloWorld {
42+
public static void main(String[] args) {
43+
System.out.println("Hello, World!");
44+
}
45+
}
46+
```
47+
<!-- langtabs-end -->
48+
49+
## Simple Function Example
50+
51+
Here's how you might define a function to calculate factorial in different languages:
52+
53+
<!-- langtabs-start -->
54+
```rust
55+
fn factorial(n: u64) -> u64 {
56+
match n {
57+
0 | 1 => 1,
58+
_ => n * factorial(n - 1)
59+
}
60+
}
61+
62+
fn main() {
63+
println!("5! = {}", factorial(5)); // Outputs: 5! = 120
64+
}
65+
```
66+
67+
```python
68+
def factorial(n):
69+
if n <= 1:
70+
return 1
71+
return n * factorial(n - 1)
72+
73+
print(f"5! = {factorial(5)}") # Outputs: 5! = 120
74+
```
75+
76+
```javascript
77+
function factorial(n) {
78+
if (n <= 1) return 1;
79+
return n * factorial(n - 1);
80+
}
81+
82+
console.log(`5! = ${factorial(5)}`); // Outputs: 5! = 120
83+
```
84+
85+
```go
86+
package main
87+
88+
import "fmt"
89+
90+
func factorial(n uint64) uint64 {
91+
if n <= 1 {
92+
return 1
93+
}
94+
return n * factorial(n-1)
95+
}
96+
97+
func main() {
98+
fmt.Printf("5! = %d\n", factorial(5)) // Outputs: 5! = 120
99+
}
100+
```
101+
102+
```java
103+
public class FactorialExample {
104+
static long factorial(int n) {
105+
if (n <= 1) return 1;
106+
return n * factorial(n - 1);
107+
}
108+
109+
public static void main(String[] args) {
110+
System.out.println("5! = " + factorial(5)); // Outputs: 5! = 120
111+
}
112+
}
113+
```
114+
<!-- langtabs-end -->
115+
116+
## Data Structures Example
117+
118+
Here's how you might implement a simple stack in different languages:
119+
120+
<!-- langtabs-start -->
121+
```rust
122+
struct Stack<T> {
123+
items: Vec<T>,
124+
}
125+
126+
impl<T> Stack<T> {
127+
fn new() -> Self {
128+
Stack { items: Vec::new() }
129+
}
130+
131+
fn push(&mut self, item: T) {
132+
self.items.push(item);
133+
}
134+
135+
fn pop(&mut self) -> Option<T> {
136+
self.items.pop()
137+
}
138+
139+
fn is_empty(&self) -> bool {
140+
self.items.is_empty()
141+
}
142+
}
143+
144+
fn main() {
145+
let mut stack = Stack::new();
146+
stack.push(1);
147+
stack.push(2);
148+
stack.push(3);
149+
150+
while let Some(item) = stack.pop() {
151+
println!("Popped: {}", item);
152+
}
153+
}
154+
```
155+
156+
```python
157+
class Stack:
158+
def __init__(self):
159+
self.items = []
160+
161+
def push(self, item):
162+
self.items.append(item)
163+
164+
def pop(self):
165+
if not self.is_empty():
166+
return self.items.pop()
167+
return None
168+
169+
def is_empty(self):
170+
return len(self.items) == 0
171+
172+
# Usage
173+
stack = Stack()
174+
stack.push(1)
175+
stack.push(2)
176+
stack.push(3)
177+
178+
while not stack.is_empty():
179+
print(f"Popped: {stack.pop()}")
180+
```
181+
182+
```javascript
183+
class Stack {
184+
constructor() {
185+
this.items = [];
186+
}
187+
188+
push(item) {
189+
this.items.push(item);
190+
}
191+
192+
pop() {
193+
if (!this.isEmpty()) {
194+
return this.items.pop();
195+
}
196+
return null;
197+
}
198+
199+
isEmpty() {
200+
return this.items.length === 0;
201+
}
202+
}
203+
204+
// Usage
205+
const stack = new Stack();
206+
stack.push(1);
207+
stack.push(2);
208+
stack.push(3);
209+
210+
while (!stack.isEmpty()) {
211+
console.log(`Popped: ${stack.pop()}`);
212+
}
213+
```
214+
215+
```go
216+
package main
217+
218+
import "fmt"
219+
220+
type Stack struct {
221+
items []int
222+
}
223+
224+
func (s *Stack) Push(item int) {
225+
s.items = append(s.items, item)
226+
}
227+
228+
func (s *Stack) Pop() (int, bool) {
229+
if s.IsEmpty() {
230+
return 0, false
231+
}
232+
233+
index := len(s.items) - 1
234+
item := s.items[index]
235+
s.items = s.items[:index]
236+
return item, true
237+
}
238+
239+
func (s *Stack) IsEmpty() bool {
240+
return len(s.items) == 0
241+
}
242+
243+
func main() {
244+
stack := Stack{}
245+
stack.Push(1)
246+
stack.Push(2)
247+
stack.Push(3)
248+
249+
for !stack.IsEmpty() {
250+
item, _ := stack.Pop()
251+
fmt.Printf("Popped: %d\n", item)
252+
}
253+
}
254+
```
255+
256+
```java
257+
import java.util.ArrayList;
258+
259+
public class StackExample {
260+
static class Stack<T> {
261+
private ArrayList<T> items = new ArrayList<>();
262+
263+
public void push(T item) {
264+
items.add(item);
265+
}
266+
267+
public T pop() {
268+
if (isEmpty()) {
269+
return null;
270+
}
271+
return items.remove(items.size() - 1);
272+
}
273+
274+
public boolean isEmpty() {
275+
return items.isEmpty();
276+
}
277+
}
278+
279+
public static void main(String[] args) {
280+
Stack<Integer> stack = new Stack<>();
281+
stack.push(1);
282+
stack.push(2);
283+
stack.push(3);
284+
285+
while (!stack.isEmpty()) {
286+
System.out.println("Popped: " + stack.pop());
287+
}
288+
}
289+
}
290+
```
291+
<!-- langtabs-end -->

0 commit comments

Comments
 (0)