From 093ae02c56779be20c9a9292a37bea099015e8cd Mon Sep 17 00:00:00 2001 From: h7x4abk3g Date: Fri, 26 Jun 2020 01:59:23 +0200 Subject: [PATCH] Rewrite some code by effective dart --- README.md | 74 +++++++------- analysis_options.yaml | 3 +- example/api/example_search.dart | 2 +- example/api/kanji_search.dart | 32 +++---- example/api/phrase_scrape.dart | 2 +- example/api/phrase_search.dart | 2 +- example/parser/parse_example_page.dart | 12 +-- example/parser/parse_kanji_page.dart | 12 +-- example/parser/parse_phrase_page.dart | 12 +-- lib/api.dart | 10 +- lib/parser.dart | 6 +- lib/src/exampleSearch.dart | 28 +++--- lib/src/kanjiSearch.dart | 128 ++++++++++++------------- lib/src/phraseScrape.dart | 46 ++++----- lib/src/phraseSearch.dart | 2 +- pubspec.yaml | 1 + 16 files changed, 187 insertions(+), 185 deletions(-) diff --git a/README.md b/README.md index 4820216..2336cad 100644 --- a/README.md +++ b/README.md @@ -31,22 +31,22 @@ import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForKanji('語').then((result) { - print('Found: ' + result.found.toString()); - print('Taught in: ' + result.taughtIn); - print('JLPT level: ' + result.jlptLevel); - print('Newspaper frequency rank: ' + result.newspaperFrequencyRank.toString()); - print('Stroke count: ' + result.strokeCount.toString()); - print('Meaning: ' + result.meaning); - print('Kunyomi: ' + jsonEncode(result.kunyomi)); - print('Kunyomi example: ' + jsonEncode(result.kunyomiExamples[0])); - print('Onyomi: ' + jsonEncode(result.onyomi)); - print('Onyomi example: ' + jsonEncode(result.onyomiExamples[0])); - print('Radical: ' + jsonEncode(result.radical)); - print('Parts: ' + jsonEncode(result.parts)); - print('Stroke order diagram: ' + result.strokeOrderDiagramUri); - print('Stroke order SVG: ' + result.strokeOrderSvgUri); - print('Stroke order GIF: ' + result.strokeOrderGifUri); - print('Jisho Uri: ' + result.uri); + print('Found: ${result.found}'); + print('Taught in: ${result.taughtIn}'); + print('JLPT level: ${result.jlptLevel}'); + print('Newspaper frequency rank: ${result.newspaperFrequencyRank}'); + print('Stroke count: ${result.strokeCount}'); + print('Meaning: ${result.meaning}'); + print('Kunyomi: ${jsonEncode(result.kunyomi)}'); + print('Kunyomi example: ${jsonEncode(result.kunyomiExamples[0])}'); + print('Onyomi: ${jsonEncode(result.onyomi)}'); + print('Onyomi example: ${jsonEncode(result.onyomiExamples[0])}'); + print('Radical: ${jsonEncode(result.radical)}'); + print('Parts: ${jsonEncode(result.parts)}'); + print('Stroke order diagram: ${result.strokeOrderDiagramUri}'); + print('Stroke order SVG: ${result.strokeOrderSvgUri}'); + print('Stroke order GIF: ${result.strokeOrderGifUri}'); + print('Jisho Uri: ${result.uri}'); }); } ``` @@ -80,10 +80,10 @@ import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForExamples('日').then((result) { - print('Jisho Uri: ' + result.uri); + print('Jisho Uri: ${result.uri}'); print(''); - for (int i = 0; i < 3; i++) { + for (var i = 0; i < 3; i++) { var example = result.results[i]; print(example.kanji); print(example.kana); @@ -124,7 +124,7 @@ This scrapes the word/phrase page on Jisho.org. This can get you some data that ```dart import 'dart:convert'; import 'package:unofficial_jisho_api/api.dart' as jisho; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); void main() async { await jisho.scrapeForPhrase('谷').then((data) { @@ -188,18 +188,18 @@ You can provide the HTML responses from Jisho yourself. This can be useful if yo ### Parse kanji page HTML ```dart -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); -const SEARCH_KANJI = '車'; -final SEARCH_URI = jisho_parser.uriForKanjiSearch(SEARCH_KANJI); +const String searchKanji = '車'; +final String searchURI = jisho_parser.uriForKanjiSearch(searchKanji); void main() async { - await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseKanjiPageData(result.body, SEARCH_KANJI); + await http.get(searchURI).then((result) { + final parsedResult = jisho_parser.parseKanjiPageData(result.body, searchKanji); print('JLPT level: ${parsedResult.jlptLevel}'); print('Stroke count: ${parsedResult.strokeCount}'); print('Meaning: ${parsedResult.meaning}'); @@ -210,18 +210,18 @@ void main() async { ### Parse example page HTML ```dart -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); -const SEARCH_EXAMPLE = '保護者'; -final SEARCH_URI = jisho_parser.uriForExampleSearch(SEARCH_EXAMPLE); +const String searchExample = '保護者'; +final String searchURI = jisho_parser.uriForExampleSearch(searchExample); void main() async { - await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseExamplePageData(result.body, SEARCH_EXAMPLE); + await http.get(searchURI).then((result) { + final parsedResult = jisho_parser.parseExamplePageData(result.body, searchExample); print('English: ${parsedResult.results[0].english}'); print('Kanji ${parsedResult.results[0].kanji}'); print('Kana: ${parsedResult.results[0].kana}'); @@ -232,19 +232,19 @@ void main() async { ### Parse phrase page HTML ```dart -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); -const SEARCH_EXAMPLE = '保護者'; -final SEARCH_URI = jisho_parser.uriForPhraseScrape(SEARCH_EXAMPLE); +const String searchExample = '保護者'; +final String searchURI = jisho_parser.uriForPhraseScrape(searchExample); void main() async { - await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parsePhrasePageData(result.body, SEARCH_EXAMPLE); + await http.get(searchURI).then((result) { + final parsedResult = jisho_parser.parsePhrasePageData(result.body, searchExample); print(encoder.convert(parsedResult)); }); } diff --git a/analysis_options.yaml b/analysis_options.yaml index a686c1b..d9ff058 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,7 +1,8 @@ # Defines a default set of lint rules enforced for # projects at Google. For details and rationale, # see https://github.com/dart-lang/pedantic#enabled-lints. -include: package:pedantic/analysis_options.yaml +# include: package:pedantic/analysis_options.yaml +include: package:effective_dart/analysis_options.yaml # For lint rules and documentation, see http://dart-lang.github.io/linter/lints. # Uncomment to specify additional rules. diff --git a/example/api/example_search.dart b/example/api/example_search.dart index 4baa4af..7861318 100644 --- a/example/api/example_search.dart +++ b/example/api/example_search.dart @@ -3,7 +3,7 @@ import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForExamples('日').then((result) { - print('Jisho Uri: ' + result.uri); + print('Jisho Uri: ${result.uri}'); print(''); for (var i = 0; i < 3; i++) { diff --git a/example/api/kanji_search.dart b/example/api/kanji_search.dart index 211bdd3..085358b 100644 --- a/example/api/kanji_search.dart +++ b/example/api/kanji_search.dart @@ -3,21 +3,21 @@ import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForKanji('語').then((result) { - print('Found: ' + result.found.toString()); - print('Taught in: ' + result.taughtIn); - print('JLPT level: ' + result.jlptLevel); - print('Newspaper frequency rank: ' + result.newspaperFrequencyRank.toString()); - print('Stroke count: ' + result.strokeCount.toString()); - print('Meaning: ' + result.meaning); - print('Kunyomi: ' + jsonEncode(result.kunyomi)); - print('Kunyomi example: ' + jsonEncode(result.kunyomiExamples[0])); - print('Onyomi: ' + jsonEncode(result.onyomi)); - print('Onyomi example: ' + jsonEncode(result.onyomiExamples[0])); - print('Radical: ' + jsonEncode(result.radical)); - print('Parts: ' + jsonEncode(result.parts)); - print('Stroke order diagram: ' + result.strokeOrderDiagramUri); - print('Stroke order SVG: ' + result.strokeOrderSvgUri); - print('Stroke order GIF: ' + result.strokeOrderGifUri); - print('Jisho Uri: ' + result.uri); + print('Found: ${result.found}'); + print('Taught in: ${result.taughtIn}'); + print('JLPT level: ${result.jlptLevel}'); + print('Newspaper frequency rank: ${result.newspaperFrequencyRank}'); + print('Stroke count: ${result.strokeCount}'); + print('Meaning: ${result.meaning}'); + print('Kunyomi: ${jsonEncode(result.kunyomi)}'); + print('Kunyomi example: ${jsonEncode(result.kunyomiExamples[0])}'); + print('Onyomi: ${jsonEncode(result.onyomi)}'); + print('Onyomi example: ${jsonEncode(result.onyomiExamples[0])}'); + print('Radical: ${jsonEncode(result.radical)}'); + print('Parts: ${jsonEncode(result.parts)}'); + print('Stroke order diagram: ${result.strokeOrderDiagramUri}'); + print('Stroke order SVG: ${result.strokeOrderSvgUri}'); + print('Stroke order GIF: ${result.strokeOrderGifUri}'); + print('Jisho Uri: ${result.uri}'); }); } \ No newline at end of file diff --git a/example/api/phrase_scrape.dart b/example/api/phrase_scrape.dart index 6062d90..6f475fd 100644 --- a/example/api/phrase_scrape.dart +++ b/example/api/phrase_scrape.dart @@ -1,6 +1,6 @@ import 'dart:convert'; import 'package:unofficial_jisho_api/api.dart' as jisho; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); void main() async { await jisho.scrapeForPhrase('谷').then((data) { diff --git a/example/api/phrase_search.dart b/example/api/phrase_search.dart index 8402c3b..809e995 100644 --- a/example/api/phrase_search.dart +++ b/example/api/phrase_search.dart @@ -1,6 +1,6 @@ import 'dart:convert'; import 'package:unofficial_jisho_api/api.dart' as jisho; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); void main() async { await jisho.searchForPhrase('日').then((result) { diff --git a/example/parser/parse_example_page.dart b/example/parser/parse_example_page.dart index 0b90182..136c7e6 100644 --- a/example/parser/parse_example_page.dart +++ b/example/parser/parse_example_page.dart @@ -1,15 +1,15 @@ -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); -const SEARCH_EXAMPLE = '保護者'; -final SEARCH_URI = jisho_parser.uriForExampleSearch(SEARCH_EXAMPLE); +const String searchExample = '保護者'; +final String searchURI = jisho_parser.uriForExampleSearch(searchExample); void main() async { - await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseExamplePageData(result.body, SEARCH_EXAMPLE); + await http.get(searchURI).then((result) { + final parsedResult = jisho_parser.parseExamplePageData(result.body, searchExample); print('English: ${parsedResult.results[0].english}'); print('Kanji ${parsedResult.results[0].kanji}'); print('Kana: ${parsedResult.results[0].kana}'); diff --git a/example/parser/parse_kanji_page.dart b/example/parser/parse_kanji_page.dart index 7f92aee..01a7e63 100644 --- a/example/parser/parse_kanji_page.dart +++ b/example/parser/parse_kanji_page.dart @@ -1,15 +1,15 @@ -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); -const SEARCH_KANJI = '車'; -final SEARCH_URI = jisho_parser.uriForKanjiSearch(SEARCH_KANJI); +const String searchKanji = '車'; +final String searchURI = jisho_parser.uriForKanjiSearch(searchKanji); void main() async { - await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseKanjiPageData(result.body, SEARCH_KANJI); + await http.get(searchURI).then((result) { + final parsedResult = jisho_parser.parseKanjiPageData(result.body, searchKanji); print('JLPT level: ${parsedResult.jlptLevel}'); print('Stroke count: ${parsedResult.strokeCount}'); print('Meaning: ${parsedResult.meaning}'); diff --git a/example/parser/parse_phrase_page.dart b/example/parser/parse_phrase_page.dart index 6adb004..2f71744 100644 --- a/example/parser/parse_phrase_page.dart +++ b/example/parser/parse_phrase_page.dart @@ -1,16 +1,16 @@ -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; -final encoder = JsonEncoder.withIndent(' '); +final JsonEncoder encoder = JsonEncoder.withIndent(' '); -const SEARCH_EXAMPLE = '保護者'; -final SEARCH_URI = jisho_parser.uriForPhraseScrape(SEARCH_EXAMPLE); +const String searchExample = '保護者'; +final String searchURI = jisho_parser.uriForPhraseScrape(searchExample); void main() async { - await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parsePhrasePageData(result.body, SEARCH_EXAMPLE); + await http.get(searchURI).then((result) { + final parsedResult = jisho_parser.parsePhrasePageData(result.body, searchExample); print(encoder.convert(parsedResult)); }); } \ No newline at end of file diff --git a/lib/api.dart b/lib/api.dart index 4d76017..bf0f58a 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -3,14 +3,14 @@ /// It provides a built-in http client and performs async requests to the server /// for different types of requests. library unofficial_jisho_api; -import './src/objects.dart'; -import 'package:http/http.dart' as http; import 'dart:convert'; +import 'package:http/http.dart' as http; -import './src/phraseSearch.dart'; -import './src/kanjiSearch.dart'; import './src/exampleSearch.dart'; +import './src/kanjiSearch.dart'; +import './src/objects.dart'; import './src/phraseScrape.dart'; +import './src/phraseSearch.dart'; /// Query the official Jisho API for a word or phrase /// @@ -53,6 +53,6 @@ Future scrapeForPhrase(String phrase) async { // ); // } - throw err; + rethrow; } } \ No newline at end of file diff --git a/lib/parser.dart b/lib/parser.dart index 6e2c316..02af2a8 100644 --- a/lib/parser.dart +++ b/lib/parser.dart @@ -5,7 +5,7 @@ /// for providing HTML. library unofficial_jisho_parser; -export './src/phraseSearch.dart'; -export './src/kanjiSearch.dart' show uriForKanjiSearch, parseKanjiPageData; export './src/exampleSearch.dart' show uriForExampleSearch, parseExamplePageData; -export './src/phraseScrape.dart' show uriForPhraseScrape, parsePhrasePageData; \ No newline at end of file +export './src/kanjiSearch.dart' show uriForKanjiSearch, parseKanjiPageData; +export './src/phraseScrape.dart' show uriForPhraseScrape, parsePhrasePageData; +export './src/phraseSearch.dart'; \ No newline at end of file diff --git a/lib/src/exampleSearch.dart b/lib/src/exampleSearch.dart index e9ed310..9f28b44 100644 --- a/lib/src/exampleSearch.dart +++ b/lib/src/exampleSearch.dart @@ -1,17 +1,17 @@ -import './baseURI.dart'; -import './objects.dart'; - import 'package:html/parser.dart'; import 'package:html/dom.dart'; -final RegExp kanjiRegex = RegExp(r'[\u4e00-\u9faf\u3400-\u4dbf]'); +import './baseURI.dart'; +import './objects.dart'; + +final RegExp _kanjiRegex = RegExp(r'[\u4e00-\u9faf\u3400-\u4dbf]'); /// Provides the URI for an example search String uriForExampleSearch(String phrase) { - return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences'; + return '$SCRAPE_BASE_URI${Uri.encodeComponent(phrase)}%23sentences'; } -List getChildrenAndSymbols(Element ul) { +List _getChildrenAndSymbols(Element ul) { final ulText = ul.text; final ulCharArray = ulText.split(''); final ulChildren = ul.children; @@ -25,7 +25,7 @@ List getChildrenAndSymbols(Element ul) { symbols += ulCharArray[offsetPointer]; offsetPointer++; } - final symbolElement = Element.html('${symbols}'); + final symbolElement = Element.html('$symbols'); result.add(symbolElement); } offsetPointer += element.text.length; @@ -33,15 +33,15 @@ List getChildrenAndSymbols(Element ul) { } if (offsetPointer + 1 != ulText.length){ final symbols = ulText.substring(offsetPointer, ulText.length-1); - final symbolElement = Element.html('${symbols}'); + final symbolElement = Element.html('$symbols'); result.add(symbolElement); } return result; } -ExampleResultData getKanjiAndKana(Element div) { +ExampleResultData _getKanjiAndKana(Element div) { final ul = div.querySelector('ul'); - final contents = getChildrenAndSymbols(ul); + final contents = _getChildrenAndSymbols(ul); var kanji = ''; var kana = ''; @@ -59,7 +59,7 @@ ExampleResultData getKanjiAndKana(Element div) { final kanaEnding = []; for (var j = unlifted.length - 1; j > 0; j -= 1) { final char = unlifted[j]; - if (!kanjiRegex.hasMatch(char)) { + if (!_kanjiRegex.hasMatch(char)) { kanaEnding.add(char); } else { break; @@ -100,8 +100,8 @@ List getPieces(Element sentenceElement) { return pieces; } -ExampleResultData parseExampleDiv(Element div) { - final result = getKanjiAndKana(div); +ExampleResultData _parseExampleDiv(Element div) { + final result = _getKanjiAndKana(div); result.english = div.querySelector('.english').text; result.pieces = getPieces(div) ?? []; @@ -113,7 +113,7 @@ ExampleResults parseExamplePageData(String pageHtml, String phrase) { final document = parse(pageHtml); final divs = document.querySelectorAll('.sentence_content'); - final results = divs.map((div) => parseExampleDiv(div)).toList(); + final results = divs.map((div) => _parseExampleDiv(div)).toList(); return ExampleResults( query: phrase, diff --git a/lib/src/kanjiSearch.dart b/lib/src/kanjiSearch.dart index d4efb94..8061df0 100644 --- a/lib/src/kanjiSearch.dart +++ b/lib/src/kanjiSearch.dart @@ -1,49 +1,49 @@ +import 'package:html_unescape/html_unescape.dart' as html_entities; + import './baseURI.dart'; import './objects.dart'; +final _htmlUnescape = html_entities.HtmlUnescape(); -import 'package:html_unescape/html_unescape.dart' as html_entities; -final htmlUnescape = html_entities.HtmlUnescape(); +const _onyomiLocatorSymbol = 'On'; +const _kunyomiLocatorSymbol = 'Kun'; -const String ONYOMI_LOCATOR_SYMBOL = 'On'; -const String KUNYOMI_LOCATOR_SYMBOL = 'Kun'; - -String removeNewlines(String str) { +String _removeNewlines(String str) { return str.replaceAll(RegExp(r'(?:\r|\n)') , '').trim(); } /// Provides the URI for a kanji search String uriForKanjiSearch(String kanji) { - return '${SCRAPE_BASE_URI}${Uri.encodeComponent(kanji)}%23kanji'; + return '$SCRAPE_BASE_URI${Uri.encodeComponent(kanji)}%23kanji'; } -String getUriForStrokeOrderDiagram(String kanji) { - return '${STROKE_ORDER_DIAGRAM_BASE_URI}${kanji.codeUnitAt(0)}_frames.png'; +String _getUriForStrokeOrderDiagram(String kanji) { + return '$STROKE_ORDER_DIAGRAM_BASE_URI${kanji.codeUnitAt(0)}_frames.png'; } -bool containsKanjiGlyph(String pageHtml, String kanji) { - final kanjiGlyphToken = '

${kanji}

'; +bool _containsKanjiGlyph(String pageHtml, String kanji) { + final kanjiGlyphToken = '

$kanji

'; return pageHtml.contains(kanjiGlyphToken); } -String getStringBetweenIndicies(String data, int startIndex, int endIndex) { +String _getStringBetweenIndicies(String data, int startIndex, int endIndex) { final result = data.substring(startIndex, endIndex); - return removeNewlines(result).trim(); + return _removeNewlines(result).trim(); } -String getStringBetweenStrings(String data, String startString, String endString) { +String _getStringBetweenStrings(String data, String startString, String endString) { final regex = RegExp('${RegExp.escape(startString)}(.*?)${RegExp.escape(endString)}', dotAll: true); final match = regex.allMatches(data).toList(); return match.isNotEmpty ? match[0].group(1).toString() : null; } -int getIntBetweenStrings(String pageHtml, String startString, String endString) { - final stringBetweenStrings = getStringBetweenStrings(pageHtml, startString, endString); +int _getIntBetweenStrings(String pageHtml, String startString, String endString) { + final stringBetweenStrings = _getStringBetweenStrings(pageHtml, startString, endString); return int.parse(stringBetweenStrings); } -List getAllGlobalGroupMatches(String str, RegExp regex) { +List _getAllGlobalGroupMatches(String str, RegExp regex) { var regexResults = regex.allMatches(str).toList(); List results = []; for (var match in regexResults) { @@ -53,59 +53,59 @@ List getAllGlobalGroupMatches(String str, RegExp regex) { return results; } -List parseAnchorsToArray(String str) { +List _parseAnchorsToArray(String str) { final regex = RegExp(r'(.*?)<\/a>'); - return getAllGlobalGroupMatches(str, regex); + return _getAllGlobalGroupMatches(str, regex); } -List getYomi(String pageHtml, String yomiLocatorSymbol) { - final yomiSection = getStringBetweenStrings(pageHtml, '
${yomiLocatorSymbol}:
', ''); - return parseAnchorsToArray(yomiSection ?? ''); +List _getYomi(String pageHtml, String yomiLocatorSymbol) { + final yomiSection = _getStringBetweenStrings(pageHtml, '
$yomiLocatorSymbol:
', ''); + return _parseAnchorsToArray(yomiSection ?? ''); } -List getKunyomi(String pageHtml) { - return getYomi(pageHtml, KUNYOMI_LOCATOR_SYMBOL); +List _getKunyomi(String pageHtml) { + return _getYomi(pageHtml, _kunyomiLocatorSymbol); } -List getOnyomi(String pageHtml) { - return getYomi(pageHtml, ONYOMI_LOCATOR_SYMBOL); +List _getOnyomi(String pageHtml) { + return _getYomi(pageHtml, _onyomiLocatorSymbol); } -List getYomiExamples(String pageHtml, String yomiLocatorSymbol) { - final locatorString = '

${yomiLocatorSymbol} reading compounds

'; - final exampleSection = getStringBetweenStrings(pageHtml, locatorString, ''); +List _getYomiExamples(String pageHtml, String yomiLocatorSymbol) { + final locatorString = '

$yomiLocatorSymbol reading compounds

'; + final exampleSection = _getStringBetweenStrings(pageHtml, locatorString, ''); if (exampleSection==null) { return null; } final regex = RegExp(r'
  • (.*?)<\/li>', dotAll: true); - final regexResults = getAllGlobalGroupMatches(exampleSection, regex).map((s) => s.trim()); + final regexResults = _getAllGlobalGroupMatches(exampleSection, regex).map((s) => s.trim()); final examples = regexResults.map((regexResult) { final examplesLines = regexResult.split('\n').map((s) => s.trim()).toList(); return YomiExample( example: examplesLines[0], reading: examplesLines[1].replaceAll('【', '').replaceAll('】', ''), - meaning: htmlUnescape.convert(examplesLines[2]), + meaning: _htmlUnescape.convert(examplesLines[2]), ); }); return examples.toList(); } -List getOnyomiExamples(String pageHtml) { - return getYomiExamples(pageHtml, ONYOMI_LOCATOR_SYMBOL); +List _getOnyomiExamples(String pageHtml) { + return _getYomiExamples(pageHtml, _onyomiLocatorSymbol); } -List getKunyomiExamples(String pageHtml) { - return getYomiExamples(pageHtml, KUNYOMI_LOCATOR_SYMBOL); +List _getKunyomiExamples(String pageHtml) { + return _getYomiExamples(pageHtml, _kunyomiLocatorSymbol); } -Radical getRadical(String pageHtml) { +Radical _getRadical(String pageHtml) { const radicalMeaningStartString = ''; const radicalMeaningEndString = ''; - var radicalMeaning = getStringBetweenStrings( + var radicalMeaning = _getStringBetweenStrings( pageHtml, radicalMeaningStartString, radicalMeaningEndString, @@ -123,7 +123,7 @@ Radical getRadical(String pageHtml) { const radicalSymbolEndString = ''; final radicalSymbolEndIndex = pageHtml.indexOf(radicalSymbolEndString, radicalSymbolStartIndex); - final radicalSymbolsString = getStringBetweenIndicies( + final radicalSymbolsString = _getStringBetweenIndicies( pageHtml, radicalSymbolStartIndex, radicalSymbolEndIndex, @@ -153,64 +153,64 @@ Radical getRadical(String pageHtml) { return null; } -List getParts(String pageHtml) { +List _getParts(String pageHtml) { const partsSectionStartString = '
    Parts:
    '; const partsSectionEndString = ''; - final partsSection = getStringBetweenStrings( + final partsSection = _getStringBetweenStrings( pageHtml, partsSectionStartString, partsSectionEndString, ); - var result = parseAnchorsToArray(partsSection); + var result = _parseAnchorsToArray(partsSection); result.sort(); return (result); } -String getSvgUri(String pageHtml) { +String _getSvgUri(String pageHtml) { var svgRegex = RegExp('\/\/.*?.cloudfront.net\/.*?.svg'); final regexResult = svgRegex.firstMatch(pageHtml).group(0).toString(); - return regexResult.isNotEmpty ? 'https:${regexResult}' : null; + return regexResult.isNotEmpty ? 'https:$regexResult' : null; } -String getGifUri(String kanji) { +String _getGifUri(String kanji) { final unicodeString = kanji.codeUnitAt(0).toRadixString(16); - final fileName = '${unicodeString}.gif'; - final animationUri = 'https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/${fileName}'; + final fileName = '$unicodeString.gif'; + final animationUri = 'https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/$fileName'; return animationUri; } -int getNewspaperFrequencyRank(String pageHtml) { - final frequencySection = getStringBetweenStrings(pageHtml, '
    ', '
    '); - return (frequencySection != null) ? int.parse(getStringBetweenStrings(frequencySection, '', '')) : null; +int _getNewspaperFrequencyRank(String pageHtml) { + final frequencySection = _getStringBetweenStrings(pageHtml, '
    ', '
    '); + return (frequencySection != null) ? int.parse(_getStringBetweenStrings(frequencySection, '', '')) : null; } /// Parses a jisho kanji search page to an object KanjiResult parseKanjiPageData(String pageHtml, String kanji) { final result = KanjiResult(); result.query = kanji; - result.found = containsKanjiGlyph(pageHtml, kanji); + result.found = _containsKanjiGlyph(pageHtml, kanji); if (result.found==false) { return result; } - result.taughtIn = getStringBetweenStrings(pageHtml, 'taught in ', ''); - result.jlptLevel = getStringBetweenStrings(pageHtml, 'JLPT level ', ''); - result.newspaperFrequencyRank = getNewspaperFrequencyRank(pageHtml); - result.strokeCount = getIntBetweenStrings(pageHtml, '', ' strokes'); - result.meaning = htmlUnescape.convert(removeNewlines(getStringBetweenStrings(pageHtml, '
    ', '
    ')).trim()); - result.kunyomi = getKunyomi(pageHtml) ?? []; - result.onyomi = getOnyomi(pageHtml) ?? []; - result.onyomiExamples = getOnyomiExamples(pageHtml) ?? []; - result.kunyomiExamples = getKunyomiExamples(pageHtml) ?? []; - result.radical = getRadical(pageHtml); - result.parts = getParts(pageHtml) ?? []; - result.strokeOrderDiagramUri = getUriForStrokeOrderDiagram(kanji); - result.strokeOrderSvgUri = getSvgUri(pageHtml); - result.strokeOrderGifUri = getGifUri(kanji); + result.taughtIn = _getStringBetweenStrings(pageHtml, 'taught in ', ''); + result.jlptLevel = _getStringBetweenStrings(pageHtml, 'JLPT level ', ''); + result.newspaperFrequencyRank = _getNewspaperFrequencyRank(pageHtml); + result.strokeCount = _getIntBetweenStrings(pageHtml, '', ' strokes'); + result.meaning = _htmlUnescape.convert(_removeNewlines(_getStringBetweenStrings(pageHtml, '
    ', '
    ')).trim()); + result.kunyomi = _getKunyomi(pageHtml) ?? []; + result.onyomi = _getOnyomi(pageHtml) ?? []; + result.onyomiExamples = _getOnyomiExamples(pageHtml) ?? []; + result.kunyomiExamples = _getKunyomiExamples(pageHtml) ?? []; + result.radical = _getRadical(pageHtml); + result.parts = _getParts(pageHtml) ?? []; + result.strokeOrderDiagramUri = _getUriForStrokeOrderDiagram(kanji); + result.strokeOrderSvgUri = _getSvgUri(pageHtml); + result.strokeOrderGifUri = _getGifUri(kanji); result.uri = uriForKanjiSearch(kanji); return result; } \ No newline at end of file diff --git a/lib/src/phraseScrape.dart b/lib/src/phraseScrape.dart index 053c61a..c888ccd 100644 --- a/lib/src/phraseScrape.dart +++ b/lib/src/phraseScrape.dart @@ -1,10 +1,10 @@ -import './objects.dart'; -import './exampleSearch.dart'; - import 'package:html/parser.dart'; import 'package:html/dom.dart'; -List getTags(Document document) { +import './exampleSearch.dart' show getPieces; +import './objects.dart'; + +List _getTags(Document document) { final List tags = []; final tagElements = document.querySelectorAll('.concept_light-tag'); @@ -16,21 +16,21 @@ List getTags(Document document) { return tags; } -List getMostRecentWordTypes(Element child) { +List _getMostRecentWordTypes(Element child) { return child.text.split(',').map((s) => s.trim().toLowerCase()).toList(); } -List getOtherForms(Element child) { +List _getOtherForms(Element child) { return child.text.split('、') .map((s) => s.replaceAll('【', '').replaceAll('】', '').split(' ')) .map((a) => (KanjiKanaPair( kanji: a[0], kana: (a.length == 2) ? a[1] : null ))).toList(); } -List getNotes(Element child) => child.text.split('\n'); +List _getNotes(Element child) => child.text.split('\n'); -String getMeaning(Element child) => child.querySelector('.meaning-meaning').text; +String _getMeaning(Element child) => child.querySelector('.meaning-meaning').text; -String getMeaningAbstract(Element child) { +String _getMeaningAbstract(Element child) { final meaningAbstract = child.querySelector('.meaning-abstract'); if (meaningAbstract == null) return null; @@ -41,13 +41,13 @@ String getMeaningAbstract(Element child) { return child.querySelector('.meaning-abstract')?.text; } -List getSupplemental(Element child) { +List _getSupplemental(Element child) { final supplemental = child.querySelector('.supplemental_info'); if (supplemental == null) return []; return supplemental.text.split(',').map((s) => s.trim()).toList(); } -List getSeeAlsoTerms(List supplemental) { +List _getSeeAlsoTerms(List supplemental) { if (supplemental == null) return []; final List seeAlsoTerms = []; @@ -61,7 +61,7 @@ List getSeeAlsoTerms(List supplemental) { return seeAlsoTerms; } -List getSentences(Element child) { +List _getSentences(Element child) { final sentenceElements = child.querySelector('.sentences')?.querySelectorAll('.sentence'); if (sentenceElements == null) return []; @@ -91,7 +91,7 @@ List getSentences(Element child) { return sentences; } -PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(Document document) { +PhrasePageScrapeResult _getMeaningsOtherFormsAndNotes(Document document) { final returnValues = PhrasePageScrapeResult( otherForms: [], notes: [] ); final meaningsWrapper = document.querySelector('.meanings-wrapper'); @@ -106,20 +106,20 @@ PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(Document document) { final child = meaningsChildren[meaningIndex]; if (child.className.contains('meaning-tags')) { - mostRecentWordTypes = getMostRecentWordTypes(child); + mostRecentWordTypes = _getMostRecentWordTypes(child); } else if (mostRecentWordTypes[0] == 'other forms') { - returnValues.otherForms = getOtherForms(child); + returnValues.otherForms = _getOtherForms(child); } else if (mostRecentWordTypes[0] == 'notes') { - returnValues.notes = getNotes(child); + returnValues.notes = _getNotes(child); } else { - final meaning = getMeaning(child); - final meaningAbstract = getMeaningAbstract(child); - final supplemental = getSupplemental(child); - final seeAlsoTerms = getSeeAlsoTerms(supplemental); - final sentences = getSentences(child); + final meaning = _getMeaning(child); + final meaningAbstract = _getMeaningAbstract(child); + final supplemental = _getSupplemental(child); + final seeAlsoTerms = _getSeeAlsoTerms(supplemental); + final sentences = _getSentences(child); meanings.add(PhraseScrapeMeaning( seeAlsoTerms: seeAlsoTerms ?? [], @@ -145,12 +145,12 @@ String uriForPhraseScrape(String searchTerm) { /// Parses a jisho word search page to an object PhrasePageScrapeResult parsePhrasePageData(String pageHtml, String query) { final document = parse(pageHtml); - final result = getMeaningsOtherFormsAndNotes(document); + final result = _getMeaningsOtherFormsAndNotes(document); result.query = query; if (!result.found) return result; result.uri = uriForPhraseScrape(query); - result.tags = getTags(document); + result.tags = _getTags(document); return result; } \ No newline at end of file diff --git a/lib/src/phraseSearch.dart b/lib/src/phraseSearch.dart index fb5da74..22eca4c 100644 --- a/lib/src/phraseSearch.dart +++ b/lib/src/phraseSearch.dart @@ -3,5 +3,5 @@ import './baseURI.dart'; /// Provides the URI for a phrase search String uriForPhraseSearch(String phrase) { - return '${JISHO_API}?keyword=${Uri.encodeComponent(phrase)}'; + return '$JISHO_API?keyword=${Uri.encodeComponent(phrase)}'; } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 0ae2aa1..ac662c1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,3 +15,4 @@ dev_dependencies: pedantic: ^1.8.0 test: ^1.8.0 path: ^1.6.0 + effective_dart: ^1.0.0