diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/content-parser.c | 32 | ||||
| -rw-r--r-- | src/content-parser.h | 1 | 
2 files changed, 31 insertions, 2 deletions
diff --git a/src/content-parser.c b/src/content-parser.c index 511d09e..3b661a8 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -20,6 +20,27 @@  // expected. feel free to improve the parser and add new features. +char* +blogc_slugify(const char *str) +{ +    if (str == NULL) +        return NULL; +    char *new_str = b_strdup(str); +    int diff = 'a' - 'A';  // just to avoid magic numbers +    for (unsigned int i = 0; new_str[i] != '\0'; i++) { +        if (new_str[i] >= 'a' && new_str[i] <= 'z') +            continue; +        if (new_str[i] >= '0' && new_str[i] <= '9') +            continue; +        if (new_str[i] >= 'A' && new_str[i] <= 'Z') +            new_str[i] += diff; +        else +            new_str[i] = '-'; +    } +    return new_str; +} + +  typedef enum {      CONTENT_START_LINE = 1,      CONTENT_EXCERPT, @@ -389,6 +410,7 @@ blogc_content_parse(const char *src, size_t *end_excerpt)      char *tmp = NULL;      char *tmp2 = NULL;      char *parsed = NULL; +    char *slug = NULL;      char d = '\0'; @@ -501,8 +523,14 @@ blogc_content_parse(const char *src, size_t *end_excerpt)                      end = is_last && c != '\n' && c != '\r' ? src_len : current;                      tmp = b_strndup(src + start, end - start);                      parsed = blogc_content_parse_inline(tmp); -                    b_string_append_printf(rv, "<h%d>%s</h%d>\n", header_level, -                        parsed, header_level); +                    slug = blogc_slugify(tmp); +                    if (slug == NULL) +                        b_string_append_printf(rv, "<h%d>%s</h%d>\n", +                            header_level, parsed, header_level); +                    else +                        b_string_append_printf(rv, "<h%d id=\"%s\">%s</h%d>\n", +                            header_level, slug, parsed, header_level); +                    free(slug);                      free(parsed);                      parsed = NULL;                      free(tmp); diff --git a/src/content-parser.h b/src/content-parser.h index 4579719..2f6b8b9 100644 --- a/src/content-parser.h +++ b/src/content-parser.h @@ -12,6 +12,7 @@  #include <stdlib.h>  #include <stdbool.h> +char* blogc_slugify(const char *str);  char* blogc_content_parse_inline(const char *src);  bool blogc_is_ordered_list_item(const char *str, size_t prefix_len);  char* blogc_content_parse(const char *src, size_t *end_excerpt);  | 
