Skip to content

Commit

Permalink
Implement and expose a source map.
Browse files Browse the repository at this point in the history
Its generation is conditioned to the setting of the
OPT_SOURCEPOS flag.

Add cmark_parser_get_first_source_extent(parser).
Add cmark_source_extent_get_start(extent).
Add cmark_source_extent_get_stop(extent).
Add cmark_source_extent_get_next(extent).
Add cmark_source_extent_get_previous(extent).
Add cmark_source_extent_get_node(extent).
Add cmark_source_extent_get_type(extent.
Add cmark_source_extent_get_type_string(extent).

API change.
  • Loading branch information
MathieuDuponchelle committed Jan 6, 2017
1 parent 5df63f1 commit 0cf5f97
Show file tree
Hide file tree
Showing 9 changed files with 755 additions and 58 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(HEADERS
houdini.h
cmark_ctype.h
render.h
source_map.h
)
set(LIBRARY_SOURCES
cmark.c
Expand All @@ -40,6 +41,7 @@ set(LIBRARY_SOURCES
houdini_html_e.c
houdini_html_u.c
cmark_ctype.c
source_map.c
${HEADERS}
)

Expand Down
170 changes: 130 additions & 40 deletions src/blocks.c

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions src/cmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ typedef enum {
CMARK_NODE_LAST_INLINE = CMARK_NODE_IMAGE,
} cmark_node_type;

typedef enum {
CMARK_EXTENT_NONE,
CMARK_EXTENT_OPENER,
CMARK_EXTENT_CLOSER,
CMARK_EXTENT_BLANK,
CMARK_EXTENT_CONTENT,
CMARK_EXTENT_PUNCTUATION,
CMARK_EXTENT_LINK_DESTINATION,
CMARK_EXTENT_LINK_TITLE,
CMARK_EXTENT_LINK_LABEL,
CMARK_EXTENT_REFERENCE_DESTINATION,
CMARK_EXTENT_REFERENCE_LABEL,
CMARK_EXTENT_REFERENCE_TITLE,
} cmark_extent_type;

/* For backwards compatibility: */
#define CMARK_NODE_HEADER CMARK_NODE_HEADING
#define CMARK_NODE_HRULE CMARK_NODE_THEMATIC_BREAK
Expand Down Expand Up @@ -93,6 +108,7 @@ typedef enum {
typedef struct cmark_node cmark_node;
typedef struct cmark_parser cmark_parser;
typedef struct cmark_iter cmark_iter;
typedef struct cmark_source_extent cmark_source_extent;

/**
* ## Custom memory allocator support
Expand Down Expand Up @@ -504,6 +520,11 @@ void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len);
CMARK_EXPORT
cmark_node *cmark_parser_finish(cmark_parser *parser);

/** Return a pointer to the first extent of the parser's source map
*/
CMARK_EXPORT
cmark_source_extent *cmark_parser_get_first_source_extent(cmark_parser *parser);

/** Parse a CommonMark document in 'buffer' of length 'len'.
* Returns a pointer to a tree of nodes. The memory allocated for
* the node tree should be released using 'cmark_node_free'
Expand All @@ -515,10 +536,44 @@ cmark_node *cmark_parse_document(const char *buffer, size_t len, int options);
/** Parse a CommonMark document in file 'f', returning a pointer to
* a tree of nodes. The memory allocated for the node tree should be
* released using 'cmark_node_free' when it is no longer needed.
* Returns NULL on error.
*/
CMARK_EXPORT
cmark_node *cmark_parse_file(FILE *f, int options);

/**
* ## Source map API
*/

/* Return the index, in bytes, of the start of this extent */
CMARK_EXPORT
size_t cmark_source_extent_get_start(cmark_source_extent *extent);

/* Return the index, in bytes, of the stop of this extent. This
* index is not included in the extent*/
CMARK_EXPORT
size_t cmark_source_extent_get_stop(cmark_source_extent *extent);

/* Return the extent immediately following 'extent' */
CMARK_EXPORT
cmark_source_extent *cmark_source_extent_get_next(cmark_source_extent *extent);

/* Return the extent immediately preceding 'extent' */
CMARK_EXPORT
cmark_source_extent *cmark_source_extent_get_previous(cmark_source_extent *extent);

/* Return the node 'extent' maps to */
CMARK_EXPORT
cmark_node *cmark_source_extent_get_node(cmark_source_extent *extent);

/* Return the type of 'extent' */
CMARK_EXPORT
cmark_extent_type cmark_source_extent_get_type(cmark_source_extent *extent);

/* Return a string representation of 'extent' */
CMARK_EXPORT
const char *cmark_source_extent_get_type_string(cmark_source_extent *extent);

/**
* ## Rendering
*/
Expand Down
Loading

0 comments on commit 0cf5f97

Please sign in to comment.