summaryrefslogtreecommitdiff
path: root/ext/syck/emitter.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/syck/emitter.c')
-rw-r--r--ext/syck/emitter.c63
1 files changed, 32 insertions, 31 deletions
diff --git a/ext/syck/emitter.c b/ext/syck/emitter.c
index a4b78b6bb6..6a5e43c9ee 100644
--- a/ext/syck/emitter.c
+++ b/ext/syck/emitter.c
@@ -339,27 +339,6 @@ syck_emitter_flush( SyckEmitter *e, long check_room )
}
/*
- * Determine headers.
- */
- if ( ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) ||
- e->stage == doc_need_header )
- {
- if ( e->use_version == 1 )
- {
- char *header = S_ALLOC_N( char, 64 );
- S_MEMZERO( header, char, 64 );
- sprintf( header, "--- %%YAML:%d.%d ", SYCK_YAML_MAJOR, SYCK_YAML_MINOR );
- (e->output_handler)( e, header, strlen( header ) );
- S_FREE( header );
- }
- else
- {
- (e->output_handler)( e, "--- ", 4 );
- }
- e->stage = doc_processing;
- }
-
- /*
* Commit buffer.
*/
if ( check_room > e->marker - e->buffer )
@@ -383,6 +362,26 @@ syck_emit( SyckEmitter *e, st_data_t n )
int indent = 0, x = 0;
SyckLevel *lvl = syck_emitter_current_level( e );
+ /*
+ * Determine headers.
+ */
+ if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) )
+ {
+ if ( e->use_version == 1 )
+ {
+ char *header = S_ALLOC_N( char, 64 );
+ S_MEMZERO( header, char, 64 );
+ sprintf( header, "--- %%YAML:%d.%d ", SYCK_YAML_MAJOR, SYCK_YAML_MINOR );
+ syck_emitter_write( e, header, strlen( header ) );
+ S_FREE( header );
+ }
+ else
+ {
+ syck_emitter_write( e, "--- ", 4 );
+ }
+ e->stage = doc_processing;
+ }
+
/* Add new level */
if ( lvl->spaces >= 0 ) {
indent = lvl->spaces + e->indent;
@@ -429,6 +428,7 @@ end_emit:
syck_emitter_pop_level( e );
if ( e->lvl_idx == 1 ) {
syck_emitter_write( e, "\n", 1 );
+ e->headless = 0;
e->stage = doc_open;
}
}
@@ -493,6 +493,7 @@ void syck_emit_indent( SyckEmitter *e )
{
int i;
SyckLevel *lvl = syck_emitter_current_level( e );
+ if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return;
if ( lvl->spaces >= 0 ) {
char *spcs = S_ALLOC_N( char, lvl->spaces + 2 );
@@ -511,8 +512,8 @@ void syck_emit_indent( SyckEmitter *e )
#define SCAN_INDENTED 2
/* Larger than the requested width? */
#define SCAN_WIDE 4
-/* Opens with whitespace? */
-#define SCAN_WHITESTART 8
+/* Opens or closes with whitespace? */
+#define SCAN_WHITEEDGE 8
/* Contains a newline */
#define SCAN_NEWLINE 16
/* Contains a single quote */
@@ -562,12 +563,18 @@ syck_scan_scalar( int req_width, char *cursor, long len )
flags |= SCAN_INDIC_S;
}
- /* ending newlines */
+ /* whitespace edges */
if ( cursor[len-1] != '\n' ) {
flags |= SCAN_NONL_E;
} else if ( len > 1 && cursor[len-2] == '\n' ) {
flags |= SCAN_MANYNL_E;
}
+ if (
+ ( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) ||
+ ( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) )
+ ) {
+ flags |= SCAN_WHITEEDGE;
+ }
/* opening doc sep */
if ( len >= 3 && strncmp( cursor, "---", 3 ) == 0 )
@@ -620,12 +627,6 @@ syck_scan_scalar( int req_width, char *cursor, long len )
flags |= SCAN_FLOWMAP;
flags |= SCAN_FLOWSEQ;
}
-
- if ( i == 0 &&
- ( cursor[i] == ' ' || cursor[i] == '\t' )
- ) {
- flags |= SCAN_WHITESTART;
- }
}
/* printf( "---STR---\n%s\nFLAGS: %d\n", cursor, flags ); */
@@ -682,7 +683,7 @@ void syck_emit_scalar( SyckEmitter *e, char *tag, enum scalar_style force_style,
/* Determine block style */
if ( scan & SCAN_NONPRINT ) {
force_style = scalar_2quote;
- } else if ( scan & SCAN_WHITESTART ) {
+ } else if ( scan & SCAN_WHITEEDGE ) {
force_style = scalar_2quote;
} else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) {
force_style = scalar_literal;