Skip to content

Commit

Permalink
feat: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
halfmoon-mind committed Jul 10, 2024
1 parent 7bc386d commit 78d837c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.15

- add comments for code

## 0.0.14

- update test
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Map<String, TextStyle> styles = StyleParser.cssToTextStyle(css);

## Converting HTML to TextSpan

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

```dart
String html = """
Expand All @@ -42,7 +42,12 @@ String html = """
<p class="large">This is large text.</p>
""";
TextSpan textSpan = StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles);
Map<String, TextStyle> tagStyles = {
'p': TextStyle(color: Colors.blue),
'strong': TextStyle(fontWeight: FontWeight.bold),
};
TextSpan textSpan = StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles, existingTagStyle: tagStyles);
```

## Example
Expand Down
40 changes: 38 additions & 2 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import 'package:flutter/material.dart';
import 'package:style_parser/style_parser.dart';

void hi() {
StyleParser.htmlTagToTextSpan("HI");
void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
String css = """
.bold { font-weight: bold; }
.italic { font-style: italic; }
.large { font-size: 24pt; }
""";

String html = """
<p class="bold">This is bold text.</p>
<p class="italic">This is italic text.</p>
<p class="large">This is large text.</p>
""";

Map<String, TextStyle> styles = StyleParser.cssToTextStyle(css);
TextSpan textSpan =
StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles);

return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('StyleParser Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: RichText(
text: textSpan,
),
),
),
);
}
}
15 changes: 15 additions & 0 deletions lib/css_to_textstyle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

part of 'style_parser.dart';

/// Internal parser class for handling the conversion logic.
class _Parser {
_Parser._();

static final instance = _Parser._();

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

/// Converts a CSS string to a map of TextStyle objects.
///
/// * [style] parameter is the CSS string to be converted.
Map<String, TextStyle> cssToTextStyle(String style) {
return _getTextStyleFromCss(style);
}
}

/// Recursively converts an HTML element and its children to a TextSpan.
TextSpan _tourChildText(
TextStyle textStyle,
Element html, [
Expand Down Expand Up @@ -169,6 +179,7 @@ TextSpan _tourChildText(
);
}

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

/// Utility enum for converting CSS font-weight values to Flutter FontWeight.
enum _FontWeight {
w100,
w200,
Expand All @@ -237,6 +249,9 @@ enum _FontWeight {
w800,
w900;

/// Converts a string representation of font-weight to a Flutter FontWeight.
///
/// * [fontWeight] parameter is the font-weight string to be converted.
static FontWeight fontWeight(String fontWeight) {
switch (fontWeight) {
case 'w100':
Expand Down
10 changes: 10 additions & 0 deletions lib/style_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import 'package:html/parser.dart' as parser;

part 'css_to_textstyle.dart';

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

static final _parser = _Parser.instance;

/// Converts an HTML string with styles to a TextSpan.
///
/// * [style] parameter is the HTML string to be converted.
/// * [existingClassStyle] parameter is an optional map of class styles.
/// * [existingTagStyle] parameter is an optional map of tag styles.
static TextSpan htmlTagToTextSpan(
String style, {
Map<String, TextStyle>? existingClassStyle,
Expand All @@ -23,6 +30,9 @@ class StyleParser {
);
}

/// Converts a CSS string to a map of TextStyle objects.
///
/// * [style] parameter is the CSS string to be converted.
static Map<String, TextStyle> cssToTextStyle(String style) {
return _parser.cssToTextStyle(style);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: style_parser
description: "StyleParser is a Flutter package that parses CSS and HTML to convert styles into Flutter's `TextStyle` and `TextSpan` for rich text formatting."
version: 0.0.14
version: 0.0.15
homepage: https://github.com/halfmoon-mind/style-parser
repository: https://github.com/halfmoon-mind/style-parser

Expand Down

0 comments on commit 78d837c

Please sign in to comment.