From 78d837c4527593ca74e64284a1cea74793de0474 Mon Sep 17 00:00:00 2001 From: halfmoon-mind <5338095@gmail.com> Date: Wed, 10 Jul 2024 10:54:03 +0900 Subject: [PATCH] feat: add comments --- CHANGELOG.md | 4 ++++ README.md | 9 +++++++-- example/example.dart | 40 +++++++++++++++++++++++++++++++++++++-- lib/css_to_textstyle.dart | 15 +++++++++++++++ lib/style_parser.dart | 10 ++++++++++ pubspec.yaml | 2 +- 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6066e7..9938818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.15 + +- add comments for code + ## 0.0.14 - update test diff --git a/README.md b/README.md index 324b592..eb14e5f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Map 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 = """ @@ -42,7 +42,12 @@ String html = """

This is large text.

"""; -TextSpan textSpan = StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles); +Map tagStyles = { + 'p': TextStyle(color: Colors.blue), + 'strong': TextStyle(fontWeight: FontWeight.bold), +}; + +TextSpan textSpan = StyleParser.htmlTagToTextSpan(html, existingClassStyle: styles, existingTagStyle: tagStyles); ``` ## Example diff --git a/example/example.dart b/example/example.dart index 796bee8..f911e04 100644 --- a/example/example.dart +++ b/example/example.dart @@ -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 = """ +

This is bold text.

+

This is italic text.

+

This is large text.

+ """; + + Map 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, + ), + ), + ), + ); + } } diff --git a/lib/css_to_textstyle.dart b/lib/css_to_textstyle.dart index 0afe0f5..e1eb881 100644 --- a/lib/css_to_textstyle.dart +++ b/lib/css_to_textstyle.dart @@ -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? existingClassStyle, @@ -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 cssToTextStyle(String style) { return _getTextStyleFromCss(style); } } +/// Recursively converts an HTML element and its children to a TextSpan. TextSpan _tourChildText( TextStyle textStyle, Element html, [ @@ -169,6 +179,7 @@ TextSpan _tourChildText( ); } +/// Parses a CSS string and converts it into a map of TextStyle objects. Map _getTextStyleFromCss(String style) { Map result = {}; RegExp exp = RegExp(r'([a-zA-Z0-9\.\#]+)\s*\{([^}]*)\}'); @@ -226,6 +237,7 @@ Map _getTextStyleFromCss(String style) { return result; } +/// Utility enum for converting CSS font-weight values to Flutter FontWeight. enum _FontWeight { w100, w200, @@ -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': diff --git a/lib/style_parser.dart b/lib/style_parser.dart index f5de330..c5a5419 100644 --- a/lib/style_parser.dart +++ b/lib/style_parser.dart @@ -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? existingClassStyle, @@ -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 cssToTextStyle(String style) { return _parser.cssToTextStyle(style); } diff --git a/pubspec.yaml b/pubspec.yaml index cee9e99..50b5854 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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