Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MathJax support and bug fix #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ options:

.c.o:
@echo CC $<
@${CC} -c ${CFLAGS} $<
@${CC} -c ${CPPFLAGS} ${CFLAGS} $<

${OBJ}: config.mk

smu: ${OBJ}
@echo LD $@
@${CC} -o $@ ${OBJ} ${LDFLAGS}

math: CPPFLAGS += -DDISPLAY_MATH_DELIMITER='$$$$' -DINLINE_MATH_DELIMITER='$$'
math: options smu

clean:
@echo cleaning
@rm -f smu ${OBJ} ${LIBOBJ} smu-${VERSION}.tar.gz
Expand Down
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ Other interesting stuff
But here is
one.

* If built with `make math`, text wrapped in `$` or `$$` is not processed
so it can be used with MathJax

embed HTML
----------

Expand Down
16 changes: 14 additions & 2 deletions smu.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#define LENGTH(x) sizeof(x)/sizeof(x[0])
#define ADDC(b,i) if (i % BUFSIZ == 0) { b = realloc(b, (i + BUFSIZ) * sizeof(char)); if (!b) eprint("Malloc failed."); } b[i]

#define QUOTE(x) #x
#define EXPAND_AND_QUOTE(x) QUOTE(x)
#define DISPLAY_MATH EXPAND_AND_QUOTE(DISPLAY_MATH_DELIMITER)
#define INLINE_MATH EXPAND_AND_QUOTE(INLINE_MATH_DELIMITER)

typedef int (*Parser)(const char *, const char *, int);
typedef struct {
char *search;
Expand Down Expand Up @@ -70,6 +75,8 @@ static Tag surround[] = {
{ "```", 0, "<code>", "</code>" },
{ "``", 0, "<code>", "</code>" },
{ "`", 0, "<code>", "</code>" },
{ DISPLAY_MATH, 0, "\\[", "\\]" },
{ INLINE_MATH, 0, "\\(", "\\)" },
{ "___", 1, "<strong><em>", "</em></strong>" },
{ "***", 1, "<strong><em>", "</em></strong>" },
{ "__", 1, "<strong>", "</strong>" },
Expand Down Expand Up @@ -645,7 +652,7 @@ dosurround(const char *begin, const char *end, int newblock) {

for (i = 0; i < LENGTH(surround); i++) {
l = strlen(surround[i].search);
if (end - begin < 2*l || strncmp(begin, surround[i].search, l) != 0)
if (l == 0 || end - begin < 2*l || strncmp(begin, surround[i].search, l) != 0)
continue;
start = begin + l;
p = start;
Expand All @@ -655,8 +662,13 @@ dosurround(const char *begin, const char *end, int newblock) {
} while (p && p[-1] == '\\');
if (p && p[-1] != '\\')
stop = p;
if (!stop || stop < start || stop >= end)
if (!stop || stop <= start || stop >= end)
continue;
if (strcmp(surround[i].search, INLINE_MATH) == 0) {
/* not inline math if span starts or ends with space or contains newline */
if (isspace(start[0]) || isspace(stop[-1]) || strchr(start, '\n') < stop)
continue;
}
fputs(surround[i].before, stdout);

/* Single space at start and end are ignored */
Expand Down