Просмотр исходного кода

rename method and add html_furi_find_tags

jblanked 1 год назад
Родитель
Сommit
860b34f248
3 измененных файлов с 59 добавлено и 5 удалено
  1. 2 2
      callback/web_crawler_callback.c
  2. 30 2
      html/html_furi.c
  3. 27 1
      html/html_furi.h

+ 2 - 2
callback/web_crawler_callback.c

@@ -752,8 +752,8 @@ static char *web_crawler_parse(DataLoaderModel *model)
                 return "Failed to load HTML response.\nPress BACK to return.";
             }
             // parse HTML response
-            FuriString *h1_tag = html_furi_parse("<h1>", returned_data, 0);
-            FuriString *p_tag = html_furi_parse("<p>", returned_data, 0);
+            FuriString *h1_tag = html_furi_find_tag("<h1>", returned_data, 0);
+            FuriString *p_tag = html_furi_find_tag("<p>", returned_data, 0);
             furi_string_free(returned_data);
             if (p_tag == NULL && h1_tag == NULL)
             {

+ 30 - 2
html/html_furi.c

@@ -42,7 +42,7 @@ static bool furi_string_sub_equals(FuriString *str, int pos, const char *needle)
  * @return A newly allocated FuriString containing the parsed content,
  *         or an empty FuriString if the tag is not found.
  */
-FuriString *html_furi_parse(const char *tag, FuriString *html, size_t index)
+FuriString *html_furi_find_tag(const char *tag, FuriString *html, size_t index)
 {
     int tag_len = strlen(tag);
 
@@ -133,7 +133,7 @@ FuriString *html_furi_parse(const char *tag, FuriString *html, size_t index)
 
     // The content spans from content_start up to matching_close_index.
     size_t content_length = matching_close_index - content_start;
-    if (memmgr_get_free_heap() < (content_length + 1 + 1024)) // 1KB buffer
+    if (memmgr_get_free_heap() < (content_length + 1 + 4096)) // 4KB buffer
     {
         FURI_LOG_E("html_furi_parse", "Not enough heap to allocate result");
         return NULL;
@@ -146,3 +146,31 @@ FuriString *html_furi_parse(const char *tag, FuriString *html, size_t index)
 
     return result;
 }
+
+/*
+ * @brief Parse all Furigana strings from an HTML tag, handling nested child tags.
+ * @param tag The HTML tag to parse (including the angle brackets).
+ * @param html The HTML string to parse (as a FuriString).
+ * @return A newly allocated FuriString containing the parsed content,
+ *         or an empty FuriString if the tag is not found.
+ */
+FuriString *html_furi_find_tags(const char *tag, FuriString *html)
+{
+    FuriString *result = furi_string_alloc();
+    size_t index = 0;
+    while (true)
+    {
+        FuriString *parsed = html_furi_find_tag(tag, html, index);
+        if (parsed == NULL)
+        {
+            break;
+        }
+        furi_string_cat(result, parsed);
+        furi_string_free(parsed);
+        // start after the strlen(tag)
+        // this is so we don't miss the inner tags
+        // I may change this to: index += furi_string_size(parsed)
+        index += strlen(tag);
+    }
+    return result;
+}

+ 27 - 1
html/html_furi.h

@@ -2,4 +2,30 @@
 #include <furi.h>
 #include <furi_hal.h>
 
-FuriString *html_furi_parse(const char *tag, FuriString *html, size_t index);
+/*
+ * @brief Parse a Furigana string from an HTML tag, handling nested child tags.
+ *
+ * This version accepts an HTML tag as a C-string (e.g., "<p>") and searches
+ * for the content inside the corresponding opening and closing tags within
+ * the provided HTML string, taking into account nested occurrences of the tag.
+ *
+ * For example, given the HTML string:
+ *     "<p><h1><p><h1>Test</h1></p></h1></p>"
+ * and searching with tag "<p>" the function will return:
+ *     "<h1><p><h1>Test</h1></p></h1>"
+ *
+ * @param tag The HTML tag to parse (including the angle brackets).
+ * @param html The HTML string to parse (as a FuriString).
+ * @return A newly allocated FuriString containing the parsed content,
+ *         or an empty FuriString if the tag is not found.
+ */
+FuriString *html_furi_find_tag(const char *tag, FuriString *html, size_t index);
+
+/*
+ * @brief Parse all Furigana strings from an HTML tag, handling nested child tags.
+ * @param tag The HTML tag to parse (including the angle brackets).
+ * @param html The HTML string to parse (as a FuriString).
+ * @return A newly allocated FuriString containing the parsed content,
+ *         or an empty FuriString if the tag is not found.
+ */
+FuriString *html_furi_find_tags(const char *tag, FuriString *html);