diff options
Diffstat (limited to 'libstorycode')
-rw-r--r-- | libstorycode/narrative.c | 44 | ||||
-rw-r--r-- | libstorycode/storycode.c | 118 | ||||
-rw-r--r-- | libstorycode/storycode.h | 4 |
3 files changed, 144 insertions, 22 deletions
diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 5dc3f7f..c7e1e31 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -592,7 +592,49 @@ Slide *narrative_get_slide_by_number(Narrative *n, int pos) /* Return the text between item p1/offset o1 and p2/o2 */ char *narrative_range_as_storycode(Narrative *n, int p1, size_t o1, int p2, size_t o2) { - return strdup(": Text"); + GOutputStream *fh; + char *text; + int i; + + fh = g_memory_output_stream_new_resizable(); + if ( fh == NULL ) return NULL; + + for ( i=p1; i<=p2; i++ ) { + + if ( ((i>p1) && (i<p2)) || !narrative_item_is_text(n, i) ) { + + narrative_write_item(n, i, fh); + + } else { + + size_t start_offs, end_offs; + GError *error = NULL; + + if ( i==p1 ) { + start_offs = o1; + } else { + start_offs = 0; + } + + if ( i==p2 ) { + end_offs = o2; + } else { + end_offs = -1; + } + + narrative_write_partial_item(n, i, start_offs, end_offs, fh); + if ( i < p2 ) g_output_stream_write(fh, "\n", 1, NULL, &error); + + } + + } + + g_output_stream_close(fh, NULL, NULL); + text = g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(fh)); + g_object_unref(fh); + + printf("SC '%s'\n", text); + return text; } diff --git a/libstorycode/storycode.c b/libstorycode/storycode.c index 139af67..42c4820 100644 --- a/libstorycode/storycode.c +++ b/libstorycode/storycode.c @@ -115,17 +115,15 @@ static void write_run_border(GOutputStream *fh, enum text_run_type t) } -static char *escape_text(const char *in) +static char *escape_text(const char *in, size_t len) { int i, j; - size_t len; size_t nl = 0; size_t np = 0; const char *esc = "*/_"; size_t n_esc = 3; char *out; - len = strlen(in); for ( i=0; i<len; i++ ) { for ( j=0; j<n_esc; j++ ) { if ( in[i] == esc[j] ) { @@ -139,7 +137,7 @@ static char *escape_text(const char *in) if ( out == NULL ) return NULL; np = 0; - for ( i=0; i<=len; i++ ) { + for ( i=0; i<len; i++ ) { for ( j=0; j<n_esc; j++ ) { if ( in[i] == esc[j] ) { out[np++] = '\\'; @@ -148,21 +146,28 @@ static char *escape_text(const char *in) } out[np++] = in[i]; } + out[np] = '\0'; return out; } +static void write_partial_run(GOutputStream *fh, struct text_run *run, size_t start, size_t len) +{ + char *escaped_str; + write_run_border(fh, run->type); + escaped_str = escape_text(run->text+start, len); + write_string(fh, escaped_str); + free(escaped_str); + write_run_border(fh, run->type); +} + + static void write_para(GOutputStream *fh, struct text_run *runs, int n_runs) { int i; for ( i=0; i<n_runs; i++ ) { - char *escaped_str; - write_run_border(fh, runs[i].type); - escaped_str = escape_text(runs[i].text); - write_string(fh, escaped_str); - free(escaped_str); - write_run_border(fh, runs[i].type); + write_partial_run(fh, &runs[i], 0, strlen(runs[i].text)); } } @@ -252,40 +257,63 @@ static int write_slide(GOutputStream *fh, Slide *s) } -static int write_item(GOutputStream *fh, struct narrative_item *item) +static int write_starter(GOutputStream *fh, struct narrative_item *item) { switch ( item->type ) { case NARRATIVE_ITEM_TEXT: /* FIXME: separate alignment */ if ( write_string(fh, ": ") ) return 1; - write_para(fh, item->runs, item->n_runs); - if ( write_string(fh, "\n") ) return 1; break; case NARRATIVE_ITEM_PRESTITLE: /* FIXME: separate alignment */ if ( write_string(fh, "PRESTITLE: ") ) return 1; - write_para(fh, item->runs, item->n_runs); - if ( write_string(fh, "\n") ) return 1; break; case NARRATIVE_ITEM_BP: /* FIXME: separate alignment */ if ( write_string(fh, "BP: ") ) return 1; - write_para(fh, item->runs, item->n_runs); - if ( write_string(fh, "\n") ) return 1; break; case NARRATIVE_ITEM_SLIDE: /* FIXME: separate slide size */ - if ( write_string(fh, "SLIDE {\n") ) return 1; + if ( write_string(fh, "SLIDE ") ) return 1; + break; + + case NARRATIVE_ITEM_EOP: + if ( write_string(fh, "ENDOFPRESENTATION") ) return 1; + break; + + } + return 0; +} + + +static int write_item(GOutputStream *fh, struct narrative_item *item) +{ + if ( write_starter(fh, item) ) return 1; + switch ( item->type ) { + + case NARRATIVE_ITEM_TEXT: + write_para(fh, item->runs, item->n_runs); + break; + + case NARRATIVE_ITEM_PRESTITLE: + write_para(fh, item->runs, item->n_runs); + break; + + case NARRATIVE_ITEM_BP: + write_para(fh, item->runs, item->n_runs); + break; + + case NARRATIVE_ITEM_SLIDE: + if ( write_string(fh, "{\n") ) return 1; if ( write_slide(fh, item->slide) ) return 1; - if ( write_string(fh, "}\n") ) return 1; + if ( write_string(fh, "}") ) return 1; break; case NARRATIVE_ITEM_EOP: - if ( write_string(fh, "ENDOFPRESENTATION\n") ) return 1; break; } @@ -293,6 +321,55 @@ static int write_item(GOutputStream *fh, struct narrative_item *item) } +int narrative_write_item(Narrative *n, int item, GOutputStream *fh) +{ + return write_item(fh, &n->items[item]); +} + + +int narrative_write_partial_item(Narrative *n, int inum, size_t start, ssize_t end, + GOutputStream *fh) +{ + struct narrative_item *item; + int r1, r2; + size_t r1offs, r2offs; + size_t r1len, r2len; + int r; + + if ( !narrative_item_is_text(n, inum) ) return narrative_write_item(n, inum, fh); + + item = &n->items[inum]; + r1 = narrative_which_run(item, start, &r1offs); + + if ( end >= 0 ) { + r2 = narrative_which_run(item, end, &r2offs); + } else { + r2 = item->n_runs - 1; + } + + r1len = strlen(item->runs[r1].text); + r2len = strlen(item->runs[r2].text); + + if ( end < 0 ) r2offs = r2len; + + if ( write_starter(fh, item) ) return 1; + + for ( r=r1; r<=r2; r++ ) { + if ( (r1==r2) && (r==r1) ) { + write_partial_run(fh, &item->runs[r], r1offs, r2offs - r1offs); + } else if ( r == r1 ) { + write_partial_run(fh, &item->runs[r], r1offs, r1len - r1offs); + } else if ( r == r2 ) { + write_partial_run(fh, &item->runs[r], 0, r2offs); + } else { + write_partial_run(fh, &item->runs[r], 0, strlen(item->runs[r].text)); + } + } + + return 0; +} + + int storycode_write_presentation(Narrative *n, GOutputStream *fh) { int i; @@ -307,6 +384,7 @@ int storycode_write_presentation(Narrative *n, GOutputStream *fh) for ( i=0; i<n->n_items; i++ ) { if ( write_item(fh, &n->items[i]) ) return 1; + if ( write_string(fh, "\n") ) return 1; } return 0; diff --git a/libstorycode/storycode.h b/libstorycode/storycode.h index cd98667..6ac2e1f 100644 --- a/libstorycode/storycode.h +++ b/libstorycode/storycode.h @@ -50,6 +50,8 @@ extern char unitc(enum length_unit unit); extern Narrative *storycode_parse_presentation(const char *sc); extern int storycode_write_presentation(Narrative *n, GOutputStream *fh); - +extern int narrative_write_item(Narrative *n, int item, GOutputStream *fh); +extern int narrative_write_partial_item(Narrative *n, int inum, size_t start, ssize_t end, + GOutputStream *fh); #endif /* STORYCODE_H */ |