|
1 | 1 | --- |
2 | | -title: "Code Blocks" |
| 2 | +title: "Code" |
3 | 3 | description: "Display inline code and code blocks" |
4 | 4 | icon: 'code' |
5 | 5 | --- |
@@ -38,4 +38,166 @@ class HelloWorld { |
38 | 38 | ``` |
39 | 39 | ```` |
40 | 40 |
|
41 | | -Visit the [Code Block page](/content/components/code) for more detailed docs. |
| 41 | +## Syntax Highlighting |
| 42 | + |
| 43 | +Put the name of your programming language after the three backticks to get syntax highlighting. |
| 44 | + |
| 45 | +We use [Prism](https://prismjs.com/#supported-languages) for syntax highlighting. [Test Drive Prism](https://prismjs.com/test.html#language=markup) lists all the languages supported. |
| 46 | + |
| 47 | +```java |
| 48 | +class HelloWorld { |
| 49 | + public static void main(String[] args) { |
| 50 | + System.out.println("Hello, World!"); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +````md |
| 56 | +```java |
| 57 | +class HelloWorld { |
| 58 | + public static void main(String[] args) { |
| 59 | + System.out.println("Hello, World!"); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | +```` |
| 64 | + |
| 65 | +## Names |
| 66 | + |
| 67 | +Add a title after the programming language to set the name of your code example. The text can be anything as long as its all in one line. |
| 68 | + |
| 69 | +```javascript Code Block Example |
| 70 | +const hello = "world"; |
| 71 | +``` |
| 72 | + |
| 73 | +````md Code Block Example |
| 74 | +```javascript Code Block Example |
| 75 | +const hello = "world"; |
| 76 | +``` |
| 77 | +```` |
| 78 | + |
| 79 | +## Line Highlighting |
| 80 | + |
| 81 | +Highlight specific lines in your code blocks by adding a special comment after the language identifier. Use curly braces `{}` and specify line numbers or ranges separated by commas. |
| 82 | + |
| 83 | +```javascript Line Highlighting Example {1,3-5} |
| 84 | +const greeting = "Hello, World!"; |
| 85 | +function sayHello() { |
| 86 | + console.log(greeting); |
| 87 | +} |
| 88 | +sayHello(); |
| 89 | +``` |
| 90 | + |
| 91 | +````md |
| 92 | +```javascript Line Highlighting Example {1,3-5} |
| 93 | +const greeting = "Hello, World!"; |
| 94 | +function sayHello() { |
| 95 | + console.log(greeting); |
| 96 | +} |
| 97 | +sayHello(); |
| 98 | +``` |
| 99 | +```` |
| 100 | + |
| 101 | +## Expandable |
| 102 | + |
| 103 | +If you have a long code block and `[expandable]` after your title to make it close and expand. |
| 104 | + |
| 105 | +```python library.py [expandable] |
| 106 | +from datetime import datetime, timedelta |
| 107 | +from typing import Dict, List, Optional |
| 108 | +from dataclasses import dataclass |
| 109 | + |
| 110 | +@dataclass |
| 111 | +class Book: |
| 112 | + title: str |
| 113 | + author: str |
| 114 | + isbn: str |
| 115 | + checked_out: bool = False |
| 116 | + due_date: Optional[datetime] = None |
| 117 | + |
| 118 | +class Library: |
| 119 | + def __init__(self): |
| 120 | + self.books: Dict[str, Book] = {} |
| 121 | + self.checkouts: Dict[str, List[str]] = {} # patron -> list of ISBNs |
| 122 | + |
| 123 | + def add_book(self, book: Book) -> None: |
| 124 | + if book.isbn in self.books: |
| 125 | + raise ValueError(f"Book with ISBN {book.isbn} already exists") |
| 126 | + self.books[book.isbn] = book |
| 127 | + |
| 128 | + def checkout_book(self, isbn: str, patron: str, days: int = 14) -> None: |
| 129 | + if patron not in self.checkouts: |
| 130 | + self.checkouts[patron] = [] |
| 131 | + |
| 132 | + book = self.books.get(isbn) |
| 133 | + if not book: |
| 134 | + raise ValueError("Book not found") |
| 135 | + |
| 136 | + if book.checked_out: |
| 137 | + raise ValueError("Book is already checked out") |
| 138 | + |
| 139 | + if len(self.checkouts[patron]) >= 3: |
| 140 | + raise ValueError("Patron has reached checkout limit") |
| 141 | + |
| 142 | + book.checked_out = True |
| 143 | + book.due_date = datetime.now() + timedelta(days=days) |
| 144 | + self.checkouts[patron].append(isbn) |
| 145 | + |
| 146 | + def return_book(self, isbn: str) -> float: |
| 147 | + book = self.books.get(isbn) |
| 148 | + if not book or not book.checked_out: |
| 149 | + raise ValueError("Book not found or not checked out") |
| 150 | + |
| 151 | + late_fee = 0.0 |
| 152 | + if datetime.now() > book.due_date: |
| 153 | + days_late = (datetime.now() - book.due_date).days |
| 154 | + late_fee = days_late * 0.50 |
| 155 | + |
| 156 | + book.checked_out = False |
| 157 | + book.due_date = None |
| 158 | + |
| 159 | + # Remove from patron's checkouts |
| 160 | + for patron, books in self.checkouts.items(): |
| 161 | + if isbn in books: |
| 162 | + books.remove(isbn) |
| 163 | + break |
| 164 | + |
| 165 | + return late_fee |
| 166 | + |
| 167 | + def search(self, query: str) -> List[Book]: |
| 168 | + query = query.lower() |
| 169 | + return [ |
| 170 | + book for book in self.books.values() |
| 171 | + if query in book.title.lower() or query in book.author.lower() |
| 172 | + ] |
| 173 | + |
| 174 | +def main(): |
| 175 | + library = Library() |
| 176 | + |
| 177 | + # Add some books |
| 178 | + books = [ |
| 179 | + Book("The Hobbit", "J.R.R. Tolkien", "978-0-261-10295-4"), |
| 180 | + Book("1984", "George Orwell", "978-0-452-28423-4"), |
| 181 | + ] |
| 182 | + |
| 183 | + for book in books: |
| 184 | + library.add_book(book) |
| 185 | + |
| 186 | + # Checkout and return example |
| 187 | + library.checkout_book("978-0-261-10295-4", "patron123") |
| 188 | + late_fee = library.return_book("978-0-261-10295-4") |
| 189 | + print(f"Late fee: ${late_fee:.2f}") |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + main() |
| 193 | +``` |
| 194 | + |
| 195 | +````md |
| 196 | +```javascript Expandable Example [expandable] |
| 197 | +const greeting = "Hello, World!"; |
| 198 | +function sayHello() { |
| 199 | + console.log(greeting); |
| 200 | +} |
| 201 | +sayHello(); |
| 202 | +``` |
| 203 | +```` |
0 commit comments