]> granicus.if.org Git - multimarkdown/commitdiff
FIXED: Improve internal link creation in LaTeX
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Thu, 1 Mar 2018 00:53:16 +0000 (19:53 -0500)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Thu, 1 Mar 2018 00:53:16 +0000 (19:53 -0500)
Sources/libMultiMarkdown/latex.c
Sources/libMultiMarkdown/writer.c
Sources/libMultiMarkdown/writer.h

index 3667e139d458aa1df7058fc79942d00bed642775..bc6b06b823fcdf57329435fdef882e3f0481d838 100644 (file)
@@ -263,14 +263,13 @@ void mmd_export_link_latex(DString * out, const char * source, token * text, lin
                        if (text && text->child) {
                                temp_char = label_from_token(source, text);
 
-                               if (strcmp(temp_char, &(link->url[1])) == 0) {
-                                       // [bar][bar] or [bar](#bar) or [bar]
-                                       printf("\\autoref{%s}", &(link->url)[1]);
-                               } else {
+                               if (temp_char && temp_char[0] != '\0') {
                                        mmd_export_token_tree_latex(out, source, text->child, scratch);
                                        print_const(" (");
                                        printf("\\autoref{%s}", &(link->url)[1]);
                                        print_const(")");
+                               } else {
+                                       printf("\\autoref{%s}", &(link->url)[1]);
                                }
 
                                free(temp_char);
index eb6abe85764c5faaac4c29ff3811d6489f97e21a..7deec97c0da4a226cd2ab17d8a3fdde1c7893487 100644 (file)
@@ -611,7 +611,7 @@ attr * parse_attributes(char * source) {
 }
 
 
-link * link_new(const char * source, token * label, char * url, char * title, char * attributes) {
+link * link_new(const char * source, token * label, char * url, char * title, char * attributes, short flags) {
        link * l = malloc(sizeof(link));
 
        if (l) {
@@ -628,6 +628,8 @@ link * link_new(const char * source, token * label, char * url, char * title, ch
                l->url = clean_string(url, false);
                l->title = (title == NULL) ? NULL : my_strdup(title);
                l->attributes = (attributes == NULL) ? NULL : parse_attributes(attributes);
+
+               l->flags = flags;
        }
 
        return l;
@@ -1029,7 +1031,7 @@ void extract_from_paren(token * paren, const char * source, char ** url, char **
 }
 
 
-/// Create a link from an explicit link `[foo](bar)`
+/// Create a link from an explicit "inline" link `[foo](bar)`
 link * explicit_link(scratch_pad * scratch, token * bracket, token * paren, const char * source) {
        char * url_char = NULL;
        char * title_char = NULL;
@@ -1040,10 +1042,10 @@ link * explicit_link(scratch_pad * scratch, token * bracket, token * paren, cons
 
        if (attr_char) {
                if (!(scratch->extensions & EXT_COMPATIBILITY)) {
-                       l = link_new(source, NULL, url_char, title_char, attr_char);
+                       l = link_new(source, NULL, url_char, title_char, attr_char, LINK_INLINE);
                }
        } else {
-               l = link_new(source, NULL, url_char, title_char, attr_char);
+               l = link_new(source, NULL, url_char, title_char, attr_char, LINK_INLINE);
        }
 
        free(url_char);
@@ -1296,12 +1298,12 @@ bool definition_extract(mmd_engine * e, token ** remainder) {
                                                }
                                        }
 
-                                       l = link_new(e->dstr->str, label, url_char, title_char, attr_char);
+                                       l = link_new(e->dstr->str, label, url_char, title_char, attr_char, LINK_REFERENCE);
                                } else {
                                        // Not valid match
                                }
                        } else {
-                               l = link_new(e->dstr->str, label, url_char, title_char, attr_char);
+                               l = link_new(e->dstr->str, label, url_char, title_char, attr_char, LINK_REFERENCE);
                        }
 
                        // Store link for later use
@@ -1516,7 +1518,7 @@ void process_header_to_links(mmd_engine * e, token * h) {
 
        d_string_append(url, label);
 
-       link * l = link_new(e->dstr->str, h, url->str, NULL, NULL);
+       link * l = link_new(e->dstr->str, h, url->str, NULL, NULL, LINK_AUTO);
 
        // Store link for later use
        stack_push(e->link_stack, l);
@@ -1553,7 +1555,7 @@ void process_table_to_link(mmd_engine * e, token * t) {
                DString * url = d_string_new("#");
                d_string_append(url, label);
 
-               link * l = link_new(e->dstr->str, temp_token, url->str, NULL, NULL);
+               link * l = link_new(e->dstr->str, temp_token, url->str, NULL, NULL, LINK_AUTO);
 
                stack_push(e->link_stack, l);
 
index dc1d5e9338e58cb3723d8926dc3803fac0a7bfd4..cb25f6fec7891267da8c2eb52eab3c246d875fb8 100644 (file)
@@ -144,9 +144,17 @@ struct link {
        char *                          url;
        char *                          title;
        attr *                          attributes;
+       short                           flags;
        UT_hash_handle          hh;
 };
 
+enum link_flags {
+       LINK_INLINE       = 1 << 0,                     //!< Inline link, e.g. [foo](#bar)
+       LINK_IMPLICIT     = 1 << 1,                     //!< Implicit link, e.g. [foo]
+       LINK_REFERENCE    = 1 << 2,                     //!< Reference definition
+       LINK_AUTO         = 1 << 3,             //!< Automatically generated link (e.g. Headers, tables)
+};
+
 typedef struct link link;
 
 struct footnote {