Skip to content

Commit 78d837c

Browse files
committed
feat: add comments
1 parent 7bc386d commit 78d837c

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.15
2+
3+
- add comments for code
4+
15
## 0.0.14
26

37
- update test

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Map<String, TextStyle> styles = StyleParser.cssToTextStyle(css);
3333

3434
## Converting HTML to TextSpan
3535

36-
To convert an HTML string with styles to a `TextSpan`:
36+
To convert an HTML string with styles to a `TextSpan` and apply existing tag styles:
3737

3838
```dart
3939
String html = """
@@ -42,7 +42,12 @@ String html = """
4242
<p class="large">This is large text.</p>
4343
""";
4444
45-
TextSpan textSpan = StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles);
45+
Map<String, TextStyle> tagStyles = {
46+
'p': TextStyle(color: Colors.blue),
47+
'strong': TextStyle(fontWeight: FontWeight.bold),
48+
};
49+
50+
TextSpan textSpan = StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles, existingTagStyle: tagStyles);
4651
```
4752

4853
## Example

example/example.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1+
import 'package:flutter/material.dart';
12
import 'package:style_parser/style_parser.dart';
23

3-
void hi() {
4-
StyleParser.htmlTagToTextSpan("HI");
4+
void main() {
5+
runApp(MyApp());
6+
}
7+
8+
class MyApp extends StatelessWidget {
9+
@override
10+
Widget build(BuildContext context) {
11+
String css = """
12+
.bold { font-weight: bold; }
13+
.italic { font-style: italic; }
14+
.large { font-size: 24pt; }
15+
""";
16+
17+
String html = """
18+
<p class="bold">This is bold text.</p>
19+
<p class="italic">This is italic text.</p>
20+
<p class="large">This is large text.</p>
21+
""";
22+
23+
Map<String, TextStyle> styles = StyleParser.cssToTextStyle(css);
24+
TextSpan textSpan =
25+
StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles);
26+
27+
return MaterialApp(
28+
home: Scaffold(
29+
appBar: AppBar(
30+
title: Text('StyleParser Example'),
31+
),
32+
body: Padding(
33+
padding: const EdgeInsets.all(16.0),
34+
child: RichText(
35+
text: textSpan,
36+
),
37+
),
38+
),
39+
);
40+
}
541
}

lib/css_to_textstyle.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
part of 'style_parser.dart';
44

5+
/// Internal parser class for handling the conversion logic.
56
class _Parser {
67
_Parser._();
78

89
static final instance = _Parser._();
910

11+
/// Converts an HTML string with styles to a TextSpan.
12+
///
13+
/// * [style] parameter is the HTML string to be converted.
14+
/// * [existingClassStyle] parameter is an optional map of class styles.
15+
/// * [existingTagStyle] parameter is an optional map of tag styles.
1016
TextSpan htmlTagToTextSpan(
1117
String style, {
1218
Map<String, TextStyle>? existingClassStyle,
@@ -26,11 +32,15 @@ class _Parser {
2632
return TextSpan(children: textSpans);
2733
}
2834

35+
/// Converts a CSS string to a map of TextStyle objects.
36+
///
37+
/// * [style] parameter is the CSS string to be converted.
2938
Map<String, TextStyle> cssToTextStyle(String style) {
3039
return _getTextStyleFromCss(style);
3140
}
3241
}
3342

43+
/// Recursively converts an HTML element and its children to a TextSpan.
3444
TextSpan _tourChildText(
3545
TextStyle textStyle,
3646
Element html, [
@@ -169,6 +179,7 @@ TextSpan _tourChildText(
169179
);
170180
}
171181

182+
/// Parses a CSS string and converts it into a map of TextStyle objects.
172183
Map<String, TextStyle> _getTextStyleFromCss(String style) {
173184
Map<String, TextStyle> result = {};
174185
RegExp exp = RegExp(r'([a-zA-Z0-9\.\#]+)\s*\{([^}]*)\}');
@@ -226,6 +237,7 @@ Map<String, TextStyle> _getTextStyleFromCss(String style) {
226237
return result;
227238
}
228239

240+
/// Utility enum for converting CSS font-weight values to Flutter FontWeight.
229241
enum _FontWeight {
230242
w100,
231243
w200,
@@ -237,6 +249,9 @@ enum _FontWeight {
237249
w800,
238250
w900;
239251

252+
/// Converts a string representation of font-weight to a Flutter FontWeight.
253+
///
254+
/// * [fontWeight] parameter is the font-weight string to be converted.
240255
static FontWeight fontWeight(String fontWeight) {
241256
switch (fontWeight) {
242257
case 'w100':

lib/style_parser.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import 'package:html/parser.dart' as parser;
66

77
part 'css_to_textstyle.dart';
88

9+
/// A utility class for parsing CSS and HTML to Flutter TextStyle and TextSpan.
10+
/// It should be used as a static class.
911
class StyleParser {
1012
StyleParser._();
1113

1214
static final _parser = _Parser.instance;
1315

16+
/// Converts an HTML string with styles to a TextSpan.
17+
///
18+
/// * [style] parameter is the HTML string to be converted.
19+
/// * [existingClassStyle] parameter is an optional map of class styles.
20+
/// * [existingTagStyle] parameter is an optional map of tag styles.
1421
static TextSpan htmlTagToTextSpan(
1522
String style, {
1623
Map<String, TextStyle>? existingClassStyle,
@@ -23,6 +30,9 @@ class StyleParser {
2330
);
2431
}
2532

33+
/// Converts a CSS string to a map of TextStyle objects.
34+
///
35+
/// * [style] parameter is the CSS string to be converted.
2636
static Map<String, TextStyle> cssToTextStyle(String style) {
2737
return _parser.cssToTextStyle(style);
2838
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: style_parser
22
description: "StyleParser is a Flutter package that parses CSS and HTML to convert styles into Flutter's `TextStyle` and `TextSpan` for rich text formatting."
3-
version: 0.0.14
3+
version: 0.0.15
44
homepage: https://github.com/halfmoon-mind/style-parser
55
repository: https://github.com/halfmoon-mind/style-parser
66

0 commit comments

Comments
 (0)