autolink: Cleanup API

This commit is contained in:
Vicent Marti 2011-06-09 02:58:06 +02:00
parent 8af37fd1b4
commit b3957282ce
5 changed files with 81 additions and 28 deletions

View file

@ -34,12 +34,6 @@ typedef enum {
HTML_USE_XHTML = (1 << 11),
} render_mode;
typedef enum {
AUTOLINK_URLS = (1 << 0),
AUTOLINK_EMAILS = (1 << 1),
AUTOLINK_ALL = AUTOLINK_URLS|AUTOLINK_EMAILS
} autolink_mode;
void
upshtml_escape(struct buf *ob, const char *src, size_t size);
@ -55,9 +49,5 @@ upshtml_free_renderer(struct mkd_renderer *renderer);
extern void
upshtml_smartypants(struct buf *ob, struct buf *text);
extern void
upshtml_autolink(struct buf *ob, struct buf *text, unsigned int autolink_flags);
#endif

View file

@ -14,15 +14,47 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "markdown.h"
#include "autolink.h"
#include "buffer.h"
#include "html.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
static void
autolink_escape_cb(struct buf *ob, const struct buf *text, void *unused)
{
size_t i = 0, org;
while (i < text->size) {
org = i;
while (i < text->size &&
text->data[i] != '<' &&
text->data[i] != '>' &&
text->data[i] != '&' &&
text->data[i] != '"')
i++;
if (i > org)
bufput(ob, text->data + org, i - org);
if (i >= text->size)
break;
switch (text->data[i]) {
case '<': BUFPUTSL(ob, "&lt;"); break;
case '>': BUFPUTSL(ob, "&gt;"); break;
case '&': BUFPUTSL(ob, "&amp;"); break;
case '"': BUFPUTSL(ob, "&quot;"); break;
default: bufputc(ob, text->data[i]); break;
}
i++;
}
}
static inline int
is_closing_a(const char *tag, size_t size)
{
@ -78,6 +110,7 @@ upshtml_autolink(
struct buf *ob,
struct buf *text,
unsigned int flags,
const char *link_attr,
void (*link_text_cb)(struct buf *ob, const struct buf *link, void *payload),
void *payload)
{
@ -104,6 +137,9 @@ upshtml_autolink(
return;
}
if (link_text_cb == NULL)
link_text_cb = &autolink_escape_cb;
bufgrow(ob, text->size);
i = end = 0;
@ -130,8 +166,7 @@ upshtml_autolink(
BUFPUTSL(ob, "<a href=\"mailto:");
bufput(ob, link->data, link->size);
BUFPUTSL(ob, "\">");
if (link_text_cb) callback(ob, link, payload);
else upshtml_escape(ob, link->data, link->size);
link_text_cb(ob, link, payload);
BUFPUTSL(ob, "</a>");
}
break;
@ -142,8 +177,7 @@ upshtml_autolink(
BUFPUTSL(ob, "<a href=\"http://");
bufput(ob, link->data, link->size);
BUFPUTSL(ob, "\">");
if (link_text_cb) callback(ob, link, payload);
else upshtml_escape(ob, link->data, link->size);
link_text_cb(ob, link, payload);
BUFPUTSL(ob, "</a>");
}
break;
@ -155,8 +189,7 @@ upshtml_autolink(
BUFPUTSL(ob, "<a href=\"");
bufput(ob, link->data, link->size);
BUFPUTSL(ob, "\">");
if (link_text_cb) callback(ob, link, payload);
else upshtml_escape(ob, link->data, link->size);
link_text_cb(ob, link, payload);
BUFPUTSL(ob, "</a>");
}
break;

View file

@ -14,7 +14,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "markdown.h"
#include "buffer.h"
#include <string.h>

39
src/autolink.h Normal file
View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2011, Vicent Marti
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef UPSKIRT_AUTOLINK_H
#define UPSKIRT_AUTOLINK_H_H
#include "buffer.h"
typedef enum {
AUTOLINK_URLS = (1 << 0),
AUTOLINK_EMAILS = (1 << 1),
AUTOLINK_ALL = AUTOLINK_URLS|AUTOLINK_EMAILS
} autolink_mode;
extern size_t
ups_autolink__www(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
extern size_t
ups_autolink__email(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
extern size_t
ups_autolink__url(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
#endif
/* vim: set filetype=c: */

View file

@ -20,6 +20,7 @@
#define UPSKIRT_MARKDOWN_H
#include "buffer.h"
#include "autolink.h"
#define UPSKIRT_VERSION "1.15.2"
#define UPSKIRT_VER_MAJOR 1
@ -105,15 +106,6 @@ struct mkd_renderer {
int
is_safe_link(const char *link, size_t link_len);
size_t
ups_autolink__www(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
size_t
ups_autolink__email(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
size_t
ups_autolink__url(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
/**********************
* EXPORTED FUNCTIONS *
**********************/