Skip to content

Commit 2f5b587

Browse files
committed
Mark up source code language of all code blocks in readme
1 parent f91b3e7 commit 2f5b587

File tree

1 file changed

+65
-41
lines changed

1 file changed

+65
-41
lines changed

README.md

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@ A Parser for CSS Files written in PHP. Allows extraction of CSS files into a dat
1111

1212
Add php-css-parser to your composer.json
1313

14-
{
15-
"require": {
16-
"sabberworm/php-css-parser": "*"
17-
}
18-
}
14+
```json
15+
{
16+
"require": {
17+
"sabberworm/php-css-parser": "*"
18+
}
19+
}
20+
```
1921

2022
### Extraction
2123

2224
To use the CSS Parser, create a new instance. The constructor takes the following form:
2325

24-
new Sabberworm\CSS\Parser($sText);
26+
```php
27+
new Sabberworm\CSS\Parser($sText);
28+
```
2529

2630
To read a file, for example, you’d do the following:
2731

28-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
29-
$oCssDocument = $oCssParser->parse();
32+
```php
33+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
34+
$oCssDocument = $oCssParser->parse();
35+
```
3036

3137
The resulting CSS document structure can be manipulated prior to being output.
3238

@@ -36,21 +42,27 @@ The resulting CSS document structure can be manipulated prior to being output.
3642

3743
The charset option is used only if no @charset declaration is found in the CSS file. UTF-8 is the default, so you won’t have to create a settings object at all if you don’t intend to change that.
3844

39-
$oSettings = Sabberworm\CSS\Settings::create()->withDefaultCharset('windows-1252');
40-
new Sabberworm\CSS\Parser($sText, $oSettings);
45+
```php
46+
$oSettings = Sabberworm\CSS\Settings::create()->withDefaultCharset('windows-1252');
47+
new Sabberworm\CSS\Parser($sText, $oSettings);
48+
```
4149

4250
#### Strict parsing
4351

4452
To have the parser choke on invalid rules, supply a thusly configured Sabberworm\CSS\Settings object:
4553

46-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'), Sabberworm\CSS\Settings::create()->beStrict());
54+
```php
55+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'), Sabberworm\CSS\Settings::create()->beStrict());
56+
```
4757

4858
#### Disable multibyte functions
4959

5060
To achieve faster parsing, you can choose to have PHP-CSS-Parser use regular string functions instead of `mb_*` functions. This should work fine in most cases, even for UTF-8 files, as all the multibyte characters are in string literals. Still it’s not recommended to use this with input you have no control over as it’s not thoroughly covered by test cases.
5161

52-
$oSettings = Sabberworm\CSS\Settings::create()->withMultibyteSupport(false);
53-
new Sabberworm\CSS\Parser($sText, $oSettings);
62+
```php
63+
$oSettings = Sabberworm\CSS\Settings::create()->withMultibyteSupport(false);
64+
new Sabberworm\CSS\Parser($sText, $oSettings);
65+
```
5466

5567
### Manipulation
5668

@@ -114,52 +126,64 @@ There are a few convenience methods on Document to ease finding, manipulating an
114126

115127
### Use `Parser` to prepend an id to all selectors
116128

117-
$sMyId = "#my_id";
118-
$oParser = new Sabberworm\CSS\Parser($sText);
119-
$oCss = $oParser->parse();
120-
foreach($oCss->getAllDeclarationBlocks() as $oBlock) {
121-
foreach($oBlock->getSelectors() as $oSelector) {
122-
//Loop over all selector parts (the comma-separated strings in a selector) and prepend the id
123-
$oSelector->setSelector($sMyId.' '.$oSelector->getSelector());
124-
}
129+
```php
130+
$sMyId = "#my_id";
131+
$oParser = new Sabberworm\CSS\Parser($sText);
132+
$oCss = $oParser->parse();
133+
foreach($oCss->getAllDeclarationBlocks() as $oBlock) {
134+
foreach($oBlock->getSelectors() as $oSelector) {
135+
//Loop over all selector parts (the comma-separated strings in a selector) and prepend the id
136+
$oSelector->setSelector($sMyId.' '.$oSelector->getSelector());
125137
}
126-
138+
}
139+
```
140+
127141
### Shrink all absolute sizes to half
128142

129-
$oParser = new Sabberworm\CSS\Parser($sText);
130-
$oCss = $oParser->parse();
131-
foreach($oCss->getAllValues() as $mValue) {
132-
if($mValue instanceof CSSSize && !$mValue->isRelative()) {
133-
$mValue->setSize($mValue->getSize()/2);
134-
}
143+
```php
144+
$oParser = new Sabberworm\CSS\Parser($sText);
145+
$oCss = $oParser->parse();
146+
foreach($oCss->getAllValues() as $mValue) {
147+
if($mValue instanceof CSSSize && !$mValue->isRelative()) {
148+
$mValue->setSize($mValue->getSize()/2);
135149
}
150+
}
151+
```
136152

137153
### Remove unwanted rules
138154

139-
$oParser = new Sabberworm\CSS\Parser($sText);
140-
$oCss = $oParser->parse();
141-
foreach($oCss->getAllRuleSets() as $oRuleSet) {
142-
$oRuleSet->removeRule('font-'); //Note that the added dash will make this remove all rules starting with font- (like font-size, font-weight, etc.) as well as a potential font-rule
143-
$oRuleSet->removeRule('cursor');
144-
}
155+
```php
156+
$oParser = new Sabberworm\CSS\Parser($sText);
157+
$oCss = $oParser->parse();
158+
foreach($oCss->getAllRuleSets() as $oRuleSet) {
159+
$oRuleSet->removeRule('font-'); //Note that the added dash will make this remove all rules starting with font- (like font-size, font-weight, etc.) as well as a potential font-rule
160+
$oRuleSet->removeRule('cursor');
161+
}
162+
```
145163

146164
### Output
147165

148166
To output the entire CSS document into a variable, just use `->render()`:
149167

150-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
151-
$oCssDocument = $oCssParser->parse();
152-
print $oCssDocument->render();
168+
```php
169+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
170+
$oCssDocument = $oCssParser->parse();
171+
print $oCssDocument->render();
172+
```
153173

154174
If you want to format the output, pass an instance of type `Sabberworm\CSS\OutputFormat`:
155175

156-
$oFormat = Sabberworm\CSS\OutputFormat::create()->indentWithSpaces(4)->setSpaceBetweenRules("\n");
157-
print $oCssDocument->render($oFormat);
176+
```php
177+
$oFormat = Sabberworm\CSS\OutputFormat::create()->indentWithSpaces(4)->setSpaceBetweenRules("\n");
178+
print $oCssDocument->render($oFormat);
179+
```
158180

159181
Or use one of the predefined formats:
160182

161-
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createPretty());
162-
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createCompact());
183+
```php
184+
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createPretty());
185+
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createCompact());
186+
```
163187

164188
To see what you can do with output formatting, look at the tests in `tests/Sabberworm/CSS/OutputFormatTest.php`.
165189

0 commit comments

Comments
 (0)