hmmlib2: re-instsate "offset" for marker/ref + fixes
fixes: - indentation - parsing bug for marker with both episode + parameter
This commit is contained in:
parent
968437d263
commit
1993e9f1dd
|
@ -45,6 +45,7 @@ typedef struct {
|
||||||
char* editor;
|
char* editor;
|
||||||
char* publisher;
|
char* publisher;
|
||||||
char* isbn;
|
char* isbn;
|
||||||
|
int offset;
|
||||||
} HMML_Reference;
|
} HMML_Reference;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -60,6 +61,7 @@ typedef struct {
|
||||||
char* marker;
|
char* marker;
|
||||||
char* parameter;
|
char* parameter;
|
||||||
char* episode;
|
char* episode;
|
||||||
|
int offset;
|
||||||
} HMML_Marker;
|
} HMML_Marker;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -356,7 +358,9 @@ static HMML_Marker _hmml_parse_marker(struct _hmml_parser* p)
|
||||||
++p->cursor;
|
++p->cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
HMML_Marker marker = {};
|
HMML_Marker marker = {
|
||||||
|
.offset = -1,
|
||||||
|
};
|
||||||
|
|
||||||
char c = *p->cursor;
|
char c = *p->cursor;
|
||||||
if(c == '~') {
|
if(c == '~') {
|
||||||
|
@ -381,8 +385,10 @@ static HMML_Marker _hmml_parse_marker(struct _hmml_parser* p)
|
||||||
++p->cursor;
|
++p->cursor;
|
||||||
size_t n = strcspn(p->cursor, " ");
|
size_t n = strcspn(p->cursor, " ");
|
||||||
marker.episode = _hmml_persist_str(p, (struct _hmml_str){ p->cursor, n });
|
marker.episode = _hmml_persist_str(p, (struct _hmml_str){ p->cursor, n });
|
||||||
p->cursor += n;
|
p->cursor += n + 1;
|
||||||
} else if(*p->cursor != ']') {
|
}
|
||||||
|
|
||||||
|
if(*p->cursor != ']') {
|
||||||
const char* end = p->cursor;
|
const char* end = p->cursor;
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
|
@ -415,7 +421,9 @@ static HMML_Marker _hmml_parse_marker(struct _hmml_parser* p)
|
||||||
|
|
||||||
static HMML_Reference _hmml_parse_ref(struct _hmml_parser* p)
|
static HMML_Reference _hmml_parse_ref(struct _hmml_parser* p)
|
||||||
{
|
{
|
||||||
HMML_Reference ref = {};
|
HMML_Reference ref = {
|
||||||
|
.offset = -1,
|
||||||
|
};
|
||||||
|
|
||||||
struct str_attr {
|
struct str_attr {
|
||||||
struct _hmml_str str;
|
struct _hmml_str str;
|
||||||
|
@ -503,7 +511,7 @@ static void _hmml_parse_timecode(struct _hmml_parser* p, HMML_Annotation* anno)
|
||||||
anno->s = s;
|
anno->s = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno)
|
static size_t _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno)
|
||||||
{
|
{
|
||||||
static char text_mem[4096];
|
static char text_mem[4096];
|
||||||
char* out = text_mem;
|
char* out = text_mem;
|
||||||
|
@ -553,9 +561,11 @@ static void _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno)
|
||||||
if(strncmp(p->cursor + 1, "ref", 3) == 0) {
|
if(strncmp(p->cursor + 1, "ref", 3) == 0) {
|
||||||
p->cursor += 4;
|
p->cursor += 4;
|
||||||
HMML_Reference ref = _hmml_parse_ref(p);
|
HMML_Reference ref = _hmml_parse_ref(p);
|
||||||
|
ref.offset = out - text_mem;
|
||||||
_hmml_persist_array(p, &anno->references, &anno->reference_count, ref);
|
_hmml_persist_array(p, &anno->references, &anno->reference_count, ref);
|
||||||
} else {
|
} else {
|
||||||
HMML_Marker m = _hmml_parse_marker(p);
|
HMML_Marker m = _hmml_parse_marker(p);
|
||||||
|
m.offset = out - text_mem;
|
||||||
_hmml_persist_array(p, &anno->markers, &anno->marker_count, m);
|
_hmml_persist_array(p, &anno->markers, &anno->marker_count, m);
|
||||||
|
|
||||||
size_t text_len = strlen(m.marker);
|
size_t text_len = strlen(m.marker);
|
||||||
|
@ -573,7 +583,9 @@ static void _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno)
|
||||||
++p->cursor;
|
++p->cursor;
|
||||||
} else {
|
} else {
|
||||||
HMML_Marker m = _hmml_parse_marker(p);
|
HMML_Marker m = _hmml_parse_marker(p);
|
||||||
|
m.offset = out - text_mem;
|
||||||
_hmml_persist_array(p, &anno->markers, &anno->marker_count, m);
|
_hmml_persist_array(p, &anno->markers, &anno->marker_count, m);
|
||||||
|
|
||||||
size_t text_len = strlen(m.marker);
|
size_t text_len = strlen(m.marker);
|
||||||
CHECKSIZE(text_len);
|
CHECKSIZE(text_len);
|
||||||
memcpy(out, m.marker, text_len);
|
memcpy(out, m.marker, text_len);
|
||||||
|
@ -592,9 +604,12 @@ static void _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno)
|
||||||
--out;
|
--out;
|
||||||
}
|
}
|
||||||
|
|
||||||
anno->text = _hmml_persist_str(p, (struct _hmml_str){ text_mem, out - text_mem });
|
size_t text_size = out - text_mem;
|
||||||
|
anno->text = _hmml_persist_str(p, (struct _hmml_str){ text_mem, text_size });
|
||||||
|
|
||||||
#undef CHECKSIZE
|
#undef CHECKSIZE
|
||||||
|
|
||||||
|
return text_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _hmml_parse_quote(struct _hmml_parser* p, HMML_Annotation* anno)
|
static void _hmml_parse_quote(struct _hmml_parser* p, HMML_Annotation* anno)
|
||||||
|
@ -647,7 +662,7 @@ static void _hmml_parse_annotations(struct _hmml_parser* p)
|
||||||
|
|
||||||
++p->cursor;
|
++p->cursor;
|
||||||
|
|
||||||
_hmml_parse_text(p, &anno);
|
int text_len = _hmml_parse_text(p, &anno);
|
||||||
|
|
||||||
if(p->cursor[0] == '[' && p->cursor[1] == ':') {
|
if(p->cursor[0] == '[' && p->cursor[1] == ':') {
|
||||||
HMML_Marker m = _hmml_parse_marker(p);
|
HMML_Marker m = _hmml_parse_marker(p);
|
||||||
|
@ -658,7 +673,7 @@ static void _hmml_parse_annotations(struct _hmml_parser* p)
|
||||||
_hmml_parse_quote(p, &anno);
|
_hmml_parse_quote(p, &anno);
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert all markers to lowercase
|
// convert all markers to lowercase, fix any out of range offsets
|
||||||
for(size_t i = 0; i < anno.marker_count; ++i) {
|
for(size_t i = 0; i < anno.marker_count; ++i) {
|
||||||
HMML_Marker* m = anno.markers + i;
|
HMML_Marker* m = anno.markers + i;
|
||||||
for(char* c = m->marker; *c; ++c) {
|
for(char* c = m->marker; *c; ++c) {
|
||||||
|
@ -666,6 +681,9 @@ static void _hmml_parse_annotations(struct _hmml_parser* p)
|
||||||
*c = (*c - ('A' - 'a'));
|
*c = (*c - ('A' - 'a'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(m->offset > text_len) {
|
||||||
|
m->offset = text_len;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_hmml_persist_array(p, &p->out.annotations, &p->out.annotation_count, anno);
|
_hmml_persist_array(p, &p->out.annotations, &p->out.annotation_count, anno);
|
||||||
|
|
Loading…
Reference in New Issue