diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-10-12 02:31:02 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-10-12 02:31:02 -0300 |
commit | 701ffe18c2f256245b4d016daafca208839a8f63 (patch) | |
tree | 6da1c4a4e5161bfa574e0c0149bb0e78346ba02d /src/content-parser.c | |
parent | 9aec1f36e80af5c06cb690bda125c628e26e5601 (diff) | |
download | blogc-701ffe18c2f256245b4d016daafca208839a8f63.tar.gz blogc-701ffe18c2f256245b4d016daafca208839a8f63.tar.bz2 blogc-701ffe18c2f256245b4d016daafca208839a8f63.zip |
content-parser: added id attributes to headers
the id is a slugified version of the (unparsed) header content.
Diffstat (limited to 'src/content-parser.c')
-rw-r--r-- | src/content-parser.c | 32 |
1 files changed, 30 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); |