### Redux Reducer and Store Initialization in JavaScript
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/js-default-parameters/output.html
This snippet illustrates how to define individual Redux reducers (`visibleTodoFilter` and `todos`) and then combine them using `combineReducers`. Finally, it shows how to create a Redux store with the combined reducer, setting up a basic state management system for a todo application.
```javascript
function visibleTodoFilter(state = 'watch', action) {
switch (action.type) {
case 'CHANGE_VISIBLE_FILTER':
return action.filter;
default:
return state;
}
}
function todos(state, action) {
switch (action.type) {
case 'ADD_TODO':
return [...state, { text: action.text, completed: false }];
case 'COMPLETE_TODO':
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], { completed: true }),
...state.slice(action.index + 1)
];
default:
return state;
}
}
import { combineReducers, createStore } from 'redux';
let reducer = combineReducers({ visibleTodoFilter, todos });
let store = createStore(reducer);
```
--------------------------------
### Install refractor package
Source: https://github.com/wooorm/refractor/blob/main/readme.md
Instructions for installing the refractor library using npm for Node.js, Deno via esm.sh, and directly in browsers using esm.sh.
```Shell
npm install refractor
```
```JavaScript
import {refractor} from 'https://esm.sh/refractor@5'
```
```HTML
```
--------------------------------
### JavaScript Regular Expression Syntax Examples
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-regex/input.txt
Illustrates various forms of regular expression definitions in JavaScript, including simple patterns, patterns with escaped forward slashes and newlines, patterns with flags (global and multiline), and examples of regex interpolation within expressions.
```JavaScript
x = /\/
x = /\n/
x = /ab\/ ab/
x = f /6 * 2/ - 3
x = f /foo * 2/gm
x = if true then /\n/ else /[.,]+/
x = ///^key-#{key}-\d+///
```
--------------------------------
### Basic HTML Heading Element
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/markdown-code/output.html
A simple HTML heading element demonstrating basic HTML structure and content representation.
```html
a
```
--------------------------------
### Define Simple Functions in CoffeeScript
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-function/input.txt
Demonstrates basic function declaration syntax in CoffeeScript for returning null, true, and squaring a number. These are common utility functions.
```CoffeeScript
returnNull = -> null
returnTrue = () -> true
square = (x) -> x * x
```
--------------------------------
### Basic JSX Element Syntax and Usage
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/js-jsx/input.txt
Illustrates fundamental JSX element creation, including self-closing tags, nested elements with children, and elements with attributes. It also shows a simple JavaScript variable declaration in context.
```JavaScript
var jsx = ;
var jsx = ;
var jsx = ......;
var jsx =
;
var x = 5;
return ();
```
--------------------------------
### Programming Language Regular Expression Literals
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-regex/output.html
Demonstrates different ways to define and use regular expression literals. Examples include escaping forward slashes, using newline characters, applying global and multiline flags, and integrating regexes into arithmetic or conditional expressions. The last example shows a pattern that might involve interpolation, common in languages like Ruby, but presented here in a general context.
```JavaScript
x = /\/
x = /\n/
x = /ab\/ ab/
x = f /6 * 2/ - 3
x = f /foo * 2/gm
x = if true then /\n/ else /[.,]+/
x = ///^key-#{key}-\d+///
```
--------------------------------
### Basic refractor usage for syntax highlighting
Source: https://github.com/wooorm/refractor/blob/main/readme.md
Demonstrates how to import refractor and use its `highlight` method to process a JavaScript string, returning a HAST (Hypertext Abstract Syntax Tree) representation. The example also shows the structure of the yielded HAST object.
```JavaScript
import {refractor} from 'refractor'
const tree = refractor.highlight('"use strict";', 'js')
console.log(tree)
```
```JavaScript
{
type: 'root',
children: [
{
type: 'element',
tagName: 'span',
properties: {className: ['token', 'string']},
children: [{type: 'text', value: '"use strict"'}]
},
{
type: 'element',
tagName: 'span',
properties: {className: ['token', 'punctuation']},
children: [{type: 'text', value: ';'}]
}
]
}
```
--------------------------------
### Python String Literal Syntax Demonstration
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/rust-strings/output.html
This snippet showcases a variety of string literal forms available in Python, including single-quoted, double-quoted, byte strings, raw strings, and raw strings with multiple hash delimiters. It also includes examples of common escape sequences and invalid unicode escapes for testing purposes.
```Python
'a';
'\n';
'\x1A';
'\u12AS';
'\U1234ASDF';
b'a';
"hello";
b"hello";
r"hello";
r###"world"###;
r##" "##\# "# "##;
```
--------------------------------
### Basic CSS Universal Selector Rule
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/markdown-code/output.html
A basic CSS rule setting the color property for all elements using the universal selector, demonstrating fundamental CSS styling.
```css
* { color: red; }
```
--------------------------------
### Basic JSX Element Syntax
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/js-jsx/output.html
Illustrates different forms of JSX element declarations in JavaScript, covering self-closing tags, elements with children, and elements with attributes. It also shows a simple variable assignment and a return statement containing JSX.
```JavaScript
var jsx = ; var jsx = ; var jsx = ......; var jsx =
; var x = 5; return ();
```
--------------------------------
### POST /task API Request Example
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/http-default/output.html
This snippet demonstrates a sample HTTP POST request to the `/task` endpoint, including standard headers and a JSON request body. It illustrates how to send data to the server and specify content type and length for a typical API interaction.
```APIDOC
POST /task?id=1 HTTP/1.1
Host: example.org
Content-Type: application/json; charset=utf-8
Content-Length: 19
{"status": "ok", "extended": true}
```
--------------------------------
### Basic HTML Heading Element
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/markdown-code/input.txt
This snippet demonstrates a fundamental HTML structure, specifically a level 1 heading tag. It's a common element used for defining main titles on a webpage.
```html
a
```
--------------------------------
### Basic CSS Universal Selector Styling
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/markdown-code/input.txt
This CSS snippet illustrates the use of the universal selector (*) to apply a style to all elements on a page. In this case, it sets the text color of every element to red.
```css
* { color: red; }
```
--------------------------------
### Rust Numeric Literals Syntax
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/rust-numbers/input.txt
Illustrates the syntax for various integer and floating-point literals in Rust. This includes standard decimal numbers, hexadecimal (0x), binary (0b), and octal (0o) representations, along with examples of type suffixes such as `usize`, `u8`, `i32`, `u16`, `f32`, and `f64` to explicitly define the literal's type.
```Rust
123;
123usize;
123_usize;
0xff00;
0xff_u8;
0b1111111110010000;
0b1111_1111_1001_0000_i32;
0o764317;
0o764317_u16;
123.0;
0.1;
0.1f32;
12E+99_f64;
```
--------------------------------
### Tail Log File in Bash
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/bash-no-numbers/output.html
Demonstrates how to use the `tail` command in a bash shell to display the last 10 lines of a specified log file. This is a common operation for monitoring real-time logs and is often used in shell scripting.
```bash
tail \-10 access.log
```
--------------------------------
### Map String Characters to ASCII Codes in CoffeeScript
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-function/input.txt
Shows how to process a string by splitting it into words and then mapping each word to the ASCII character code of its first character. This uses array `map` for transformation.
```CoffeeScript
str.split(" ").map((m) -> m.charCodeAt(0))
```
--------------------------------
### HTTP POST Request with JSON Payload and Response
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/http-default/input.txt
This snippet illustrates a standard HTTP POST request to the '/task' endpoint, including common headers like 'Host', 'Content-Type', and 'Content-Length'. It also shows a sample JSON request body and a corresponding JSON response. This pattern is typical for RESTful API interactions.
```APIDOC
POST /task?id=1 HTTP/1.1
Host: example.org
Content-Type: application/json; charset=utf-8
Content-Length: 19
{"status": "ok", "extended": true}
```
--------------------------------
### JavaScript Division and Regular Expression Literal Parsing
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-division/output.html
These examples demonstrate various syntaxes involving the division operator (/) and regular expression literals in JavaScript. They highlight potential ambiguities or specific parsing rules when a forward slash could be interpreted as either a division operator or the start/end of a regex literal, especially when followed by flags or other operators. This is crucial for syntax highlighting and parsing tools.
```JavaScript
x = 6/foo/i
```
```JavaScript
x = 6 /foo
```
```JavaScript
x = 6 / foo
```
```JavaScript
x = 6 /foo * 2/gm
```
```JavaScript
x = f /foo
```
```JavaScript
x = f / foo / gm
```
```JavaScript
x = f /foo * 2/6
```
--------------------------------
### Node.js Basic Variable Declaration
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/js-shebang/output.html
This snippet demonstrates a basic variable declaration in Node.js. It initializes a variable 'a' with the integer value 1. This is a fundamental operation in JavaScript for storing data.
```JavaScript
#!/usr/bin/env node var a = 1;
```
--------------------------------
### Redux Reducers and Store Setup in JavaScript
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/js-default-parameters/input.txt
This snippet defines two Redux reducers, `visibleTodoFilter` and `todos`, which manage different parts of an application's state. It then demonstrates how to combine these reducers using `combineReducers` and create a Redux store with `createStore`. This pattern is fundamental for managing application state in Redux applications.
```JavaScript
function visibleTodoFilter(state = 'watch', action) {
switch (action.type) {
case 'CHANGE_VISIBLE_FILTER':
return action.filter;
default:
return state;
}
}
function todos(state, action) {
switch (action.type) {
case 'ADD_TODO':
return [...state, {
text: action.text,
completed: false
}];
case 'COMPLETE_TODO':
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
default:
return state;
}
}
import { combineReducers, createStore } from 'redux';
let reducer = combineReducers({ visibleTodoFilter, todos });
let store = createStore(reducer);
```
--------------------------------
### Groovy Class Definition and Property Access
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/groovy-fixture/input.txt
This snippet defines a simple Groovy class `AGroovyBean` with a `color` property. It then demonstrates object instantiation and two common ways to access and modify properties in Groovy: using explicit `setColor`/`getColor` methods (which are automatically generated by Groovy) and using Groovy's direct property access syntax, which is syntactic sugar for the underlying getter/setter calls.
```Groovy
class AGroovyBean {
String color
}
def myGroovyBean = new AGroovyBean()
myGroovyBean.setColor('baby blue')
assert myGroovyBean.getColor() == 'baby blue'
myGroovyBean.color = 'pewter'
assert myGroovyBean.color == 'pewter'
```
--------------------------------
### Lisp: Handling Spaces and Newlines in Expressions
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/haskell-nested-comments/lisp-mec/input.txt
This Lisp-like snippet illustrates how spaces and newlines might be interpreted within a multi-line expression or literal. It demonstrates a syntax pattern that a parser or syntax highlighter would need to correctly process, showing a literal block containing 'spaces and newlines' followed by a variable 'x'.
```Lisp
(|spaces and
newlines| x)
```
--------------------------------
### Define Protocol and Class in Swift
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/swift-functions/output.html
This Swift code snippet illustrates the definition of a protocol named `Protocol` with two function signatures (`f1()` and `f2()`), and a class named `MyClass` containing a method `f()` that returns a boolean value.
```Swift
protocol Protocol { func f1() func f2() } class MyClass { func f() { return true } }
```
--------------------------------
### Rust Variable Declarations
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/rust-variables/output.html
Demonstrates fundamental variable declaration syntax in Rust. This includes declaring an immutable variable (`let`), a mutable variable (`let mut`), and an unused variable (prefixed with `_`) to suppress compiler warnings.
```Rust
let foo; let mut bar; let _foo_bar;
```
--------------------------------
### CSS `not()` Pseudo-Class Selectors
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/css-pseudo-selector/input.txt
Demonstrates the use of the CSS `:not()` pseudo-class to select elements that do not match a specified selector. It shows both single and chained `:not()` selectors for targeting list items based on their class attributes.
```css
li:not(.red){}
li:not(.red):not(.green){}
```
--------------------------------
### Basic JSX Element Syntax
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/jsx/output.html
This snippet illustrates various forms of JSX elements, including self-closing tags, elements with single or multiple children, elements with spread attributes, and elements with explicit attributes. It covers common patterns found in React or similar JSX-based frameworks.
```JavaScript
var jsx = ;
var jsx = ;
var jsx = ......;
var jsx =
;
var x = 5;
return ();
```
--------------------------------
### Declaring and Using JSX Elements
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/jsx/input.txt
This snippet showcases different forms of JSX element declarations, ranging from simple self-closing tags to elements with nested children and attributes. It also includes a basic variable assignment and a return statement demonstrating JSX usage within a function context.
```JavaScript
var jsx = ;
var jsx = ;
var jsx = ......;
var jsx =
;
var x = 5;
return ();
```
--------------------------------
### Refractor Language Aliasing (`refractor.alias`)
Source: https://github.com/wooorm/refractor/blob/main/readme.md
Registers new aliases for already registered programming languages within Refractor. This allows highlighting content using alternative names for a language. The example shows how to add multiple aliases for 'markdown' and then use one of them for highlighting.
```APIDOC
refractor.alias(name[, alias])
Signatures:
- alias(name, alias | list)
- alias(aliases)
Parameters:
- language (`string`): programming language name
- alias (`string`): new alias for the programming language
- list (`Array`): list of aliases
- aliases (`Record`): map of `language`s to `alias`es or `list`s
Example:
import markdown from 'refractor/markdown'
import {refractor} from 'refractor/core'
refractor.register(markdown)
// refractor.highlight('*Emphasis*', 'mdown')
// ^ would throw: Error: Unknown language: `mdown` is not registered
refractor.alias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']})
refractor.highlight('*Emphasis*', 'mdown')
// ^ Works!
```
--------------------------------
### Read and Parse JSON File Asynchronously in CoffeeScript
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-function/input.txt
Demonstrates how to read a local JSON file ('package.json') asynchronously using Node.js's `fs.readFile` in CoffeeScript. It includes error handling, parsing the content, and accessing a specific property ('version').
```CoffeeScript
fs.readFile("package.json", "utf-8", (err, content) ->
data = JSON.parse(content)
data.version
)
```
--------------------------------
### Define a Function that Throws an Error in CoffeeScript
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/coffeescript-function/input.txt
Illustrates how to define a CoffeeScript function that immediately throws a new Error object. This pattern is often used for unimplemented methods or to signal an invalid state.
```CoffeeScript
npmWishlist.sha256 = (str) ->
throw new Error()
```
--------------------------------
### Groovy List Unique Elements Assertion
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/groovy-fixture/input.txt
This snippet demonstrates how to use Groovy's `first()` and `unique()` methods on a list of lists to assert that duplicate elements are removed from the first inner list, resulting in a list of unique elements.
```Groovy
assert [[1,2,3,3,3,3,4]].first().unique() == [1,2,3]
```
--------------------------------
### View Last Lines of a Log File in Bash
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/bash-no-numbers/input.txt
This snippet demonstrates how to view the last 10 lines of a specified log file using the `tail` command in a Bash shell. It's commonly used for monitoring recent activity in server logs or other frequently updated files.
```bash
tail -10 access.log
```
--------------------------------
### Rust Variable Declaration Syntax
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/rust-variables/input.txt
This code snippet demonstrates fundamental variable declaration syntax in Rust. It shows how to declare an immutable variable using `let`, a mutable variable using `let mut`, and an unused variable by prefixing its name with an underscore (`_`).
```Rust
let foo;
let mut bar;
let _foo_bar;
```
--------------------------------
### Groovy Class Definition, Property Access, and List Operations
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/groovy-fixture/output.html
This snippet illustrates how to define a simple Groovy class, instantiate an object, and interact with its properties using both traditional getter/setter methods and Groovy's direct property access. It also shows an example of list manipulation using `first()` and `unique()` methods, along with Groovy's `assert` keyword for testing conditions.
```Groovy
@BaseScript MyBaseClass baseScript @DelegatesTo(EmailSpec) assert [[1,2,3,3,3,3,4]].first().unique() == [1,2,3] class AGroovyBean { String color } def myGroovyBean = new AGroovyBean() myGroovyBean.setColor('baby blue') assert myGroovyBean.getColor() == 'baby blue' myGroovyBean.color = 'pewter' assert myGroovyBean.color == 'pewter'
```
--------------------------------
### Lisp: Using Quoted Literals
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/haskell-nested-comments/lisp-mec/input.txt
This Lisp-like snippet demonstrates the use of a single-quoted literal. In Lisp, a single quote typically prevents evaluation and treats the following expression as data rather than code, which is a fundamental concept for symbolic programming and metaprogramming.
```Lisp
(x '|quoted|)
```
--------------------------------
### CSS :not() Pseudo-class Examples
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/css-pseudo-selector/output.html
These CSS snippets illustrate how to use the `:not()` pseudo-class to select elements that do not possess a specified class. The first rule targets list items without the 'red' class, while the second targets list items without both 'red' and 'green' classes.
```css
li:not(.red){}
li:not(.red):not(.green){}
```
--------------------------------
### Define CSS Styles for Class
Source: https://github.com/wooorm/refractor/blob/main/test/fixtures/xml-sublanguages/input.txt
This snippet defines a basic CSS rule for the class `foo`, setting its color property to red. It demonstrates how to embed styling directly within an HTML document using a `