// code block cheat sheet
Quick reference for adding syntax-highlighted code to your Blogger posts. Always use HTML view when inserting code.
Basic Template
Copy this structure and replace language-xxx with your language and add your code inside:
<pre class="line-numbers"><code class="language-python">
# your code here
print("hello world")
</code></pre>
line-numbers on the <pre> tag = show line numbers. Remove it if you don't want them.
Language Classes
Replace language-python with the right one for your post:
language-python | Python — for algorithms, ML, data science |
language-javascript | JavaScript — web, Node.js |
language-typescript | TypeScript |
language-css | CSS |
language-html | HTML / markup |
language-java | Java |
language-c | C |
language-cpp | C++ |
language-csharp | C# |
language-r | R — statistics, data analysis |
language-sql | SQL — database queries |
language-bash | Bash / terminal / shell commands |
language-json | JSON — data structures, APIs |
language-yaml | YAML — config files |
language-markdown | Markdown |
language-latex | LaTeX source |
language-pseudocode | Pseudocode (plain text fallback) |
Ready-to-Copy Examples
Python — function
<pre class="line-numbers"><code class="language-python">
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5)) # 120
</code></pre>
Python — ML / NumPy style
<pre class="line-numbers"><code class="language-python">
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
mean = np.mean(X, axis=0)
print(mean) # [3. 4.]
</code></pre>
JavaScript
<pre class="line-numbers"><code class="language-javascript">
const fibonacci = (n) => {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
};
console.log(fibonacci(10)); // 55
</code></pre>
CSS
<pre class="line-numbers"><code class="language-css">
.container {
display: flex;
align-items: center;
gap: 1rem;
background-color: #0a192f;
}
</code></pre>
SQL
<pre class="line-numbers"><code class="language-sql">
SELECT name, COUNT(*) AS total
FROM students
WHERE grade >= 85
GROUP BY name
ORDER BY total DESC;
</code></pre>
Bash / Terminal
<pre class="line-numbers"><code class="language-bash">
# Install dependencies
pip install numpy pandas matplotlib scikit-learn
# Run script
python train_model.py --epochs 50 --lr 0.001
</code></pre>
R — Statistics
<pre class="line-numbers"><code class="language-r">
data <- c(12, 15, 18, 22, 25, 30)
mean(data) # 20.33
sd(data) # 6.28
summary(data)
</code></pre>
Inline Code (inside a sentence)
For short code references inside a paragraph — variable names, function names, commands:
<code>variable_name</code>
Example: "The <code>len()</code> function returns the number of items in a list."
⚠ Characters to Escape in HTML View
These characters break HTML if typed directly inside a code block. Use the escaped version:
< | < less than sign — very common in Python, JS comparisons |
> | > greater than sign — arrow functions, comparisons |
& | & ampersand — logical AND in some languages |
" | " double quote — use inside HTML attributes |
Tip: If your code block looks broken after saving, a stray
< or > is usually the culprit. Replace them with < and >.Combining Code + Math in One Post
This is common for CS/AI posts. You can mix both freely — just use the right wrapper for each:
/* Math equation */
$$f(x) = \frac{1}{1 + e^{-x}}$$
/* Then right after, the implementation */
<pre class="line-numbers"><code class="language-python">
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
</code></pre>
Pseudocode / Algorithm Steps
For writing algorithms before implementing them — use plain text with no language class, or language-text:
<pre class="line-numbers"><code class="language-text">
Algorithm: Binary Search
Input: sorted array A, target value t
Output: index of t in A, or -1
1. Set low = 0, high = len(A) - 1
2. While low <= high:
a. mid = floor((low + high) / 2)
b. If A[mid] == t: return mid
c. If A[mid] < t: low = mid + 1
d. Else: high = mid - 1
3. Return -1
</code></pre>
0 comments