Skip to content

Commit

Permalink
Tweak previous.
Browse files Browse the repository at this point in the history
Reference #140
  • Loading branch information
kristapsdz committed Sep 15, 2024
1 parent 4d35503 commit c033a68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
13 changes: 6 additions & 7 deletions gemini.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ rndr_link_ref(const struct gemini *st,
static int
rndr_escape(struct lowdown_buf *out, const char *buf, size_t sz)
{
size_t i, start = 0;
size_t i, start = 0;
unsigned char ch;

for (i = 0; i < sz; i++) {
if (buf[i] == '\n') {
ch = (unsigned char)buf[i];
if (ch == '\n') {
if (!hbuf_put(out, buf + start, i - start))
return 0;
if (out->size &&
Expand All @@ -180,18 +182,15 @@ rndr_escape(struct lowdown_buf *out, const char *buf, size_t sz)
if (!hbuf_putc(out, ' '))
return 0;
start = i + 1;
} else if ((unsigned char) buf[i] < 0x80 && iscntrl((unsigned char)buf[i])) {
fprintf(stderr, "this is a control character: %x %x\n", (unsigned char)buf[i], buf[i]);
} else if (ch < 0x80 && iscntrl(ch)) {
if (!hbuf_put(out, buf + start, i - start))
return 0;
start = i + 1;
}
}

if (start < sz &&
!hbuf_put(out, buf + start, sz - start))
if (start < sz && !hbuf_put(out, buf + start, sz - start))
return 0;

return 1;
}

Expand Down
15 changes: 9 additions & 6 deletions term.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,25 @@ static ssize_t
rndr_escape(struct term *term, struct lowdown_buf *out,
const char *buf, size_t sz)
{
size_t i, start = 0, cols = 0;
ssize_t ret;
size_t i, start = 0, cols = 0;
ssize_t ret;
unsigned char ch;

/* Don't allow control characters through. */

for (i = 0; i < sz; i++)
if ((unsigned char)buf[i] < 0x80 && iscntrl((unsigned char)buf[i])) {
ret = rndr_mbswidth
(term, buf + start, i - start);
for (i = 0; i < sz; i++) {
ch = (unsigned char)buf[i];
if (ch < 0x80 && iscntrl(ch)) {
ret = rndr_mbswidth (term, buf + start,
i - start);
if (ret < 0)
return -1;
cols += ret;
if (!hbuf_put(out, buf + start, i - start))
return -1;
start = i + 1;
}
}

/* Remaining bytes. */

Expand Down
15 changes: 9 additions & 6 deletions tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,28 @@ rndr_indent(struct lowdown_buf *ob, size_t indent)
static int
rndr_short(struct lowdown_buf *ob, const struct lowdown_buf *b)
{
size_t i;
size_t i;
unsigned char ch;

for (i = 0; i < 20 && i < b->size; i++)
if (b->data[i] == '\n') {
for (i = 0; i < 20 && i < b->size; i++) {
ch = (unsigned char)b->data[i];
if (ch == '\n') {
if (!HBUF_PUTSL(ob, "\\n"))
return 0;
} else if (b->data[i] == '\r') {
} else if (ch == '\r') {
if (!HBUF_PUTSL(ob, "\\r"))
return 0;
} else if (b->data[i] == '\t') {
} else if (ch == '\t') {
if (!HBUF_PUTSL(ob, "\\t"))
return 0;
} else if ((unsigned char)b->data[i] < 0x80 && iscntrl((unsigned char)b->data[i])) {
} else if (ch < 0x80 && iscntrl(ch)) {
if (!hbuf_putc(ob, '?'))
return 0;
} else {
if (!hbuf_putc(ob, b->data[i]))
return 0;
}
}

if (i < b->size && !HBUF_PUTSL(ob, "..."))
return 0;
Expand Down

0 comments on commit c033a68

Please sign in to comment.