pmh_styleparser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* PEG Markdown Highlight
  2. * Copyright 2011-2016 Ali Rantakari -- http://hasseg.org
  3. * Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
  4. *
  5. * pmh_styleparser.h
  6. *
  7. * Public interface of a parser for custom syntax highlighting stylesheets.
  8. */
  9. /** \file
  10. * \brief Style parser public interface.
  11. */
  12. #include "pmh_definitions.h"
  13. #include <stdbool.h>
  14. /**
  15. * \brief Color (ARGB) attribute value.
  16. *
  17. * All values are 0-255.
  18. */
  19. typedef struct
  20. {
  21. int red; /**< Red color component (0-255) */
  22. int green; /**< Green color component (0-255) */
  23. int blue; /**< Blue color component (0-255) */
  24. int alpha; /**< Alpha (opacity) color component (0-255) */
  25. } pmh_attr_argb_color;
  26. /** \brief Font style attribute value. */
  27. typedef struct
  28. {
  29. bool italic;
  30. bool bold;
  31. bool underlined;
  32. } pmh_attr_font_styles;
  33. /** \brief Font size attribute value. */
  34. typedef struct
  35. {
  36. int size_pt; /**< The font point size */
  37. bool is_relative; /**< Whether the size is relative (i.e. size_pt points
  38. larger than the default font) */
  39. } pmh_attr_font_size;
  40. /** \brief Style attribute types. */
  41. typedef enum
  42. {
  43. pmh_attr_type_foreground_color, /**< Foreground color */
  44. pmh_attr_type_background_color, /**< Background color */
  45. pmh_attr_type_caret_color, /**< Caret (insertion point) color */
  46. pmh_attr_type_font_size_pt, /**< Font size (in points) */
  47. pmh_attr_type_font_family, /**< Font family */
  48. pmh_attr_type_font_style, /**< Font style */
  49. pmh_attr_type_strike_color, /**< Strike-through color */
  50. pmh_attr_type_other /**< Arbitrary custom attribute */
  51. } pmh_attr_type;
  52. /**
  53. * \brief Style attribute value.
  54. *
  55. * Determine which member to access in this union based on the
  56. * 'type' value of the pmh_style_attribute.
  57. *
  58. * \sa pmh_style_attribute
  59. */
  60. typedef union
  61. {
  62. pmh_attr_argb_color *argb_color; /**< ARGB color */
  63. pmh_attr_font_styles *font_styles; /**< Font styles */
  64. pmh_attr_font_size *font_size; /**< Font size */
  65. char *font_family; /**< Font family */
  66. char *string; /**< Arbitrary custom string value
  67. (use this if the attribute's type
  68. is pmh_attr_type_other) */
  69. } pmh_attr_value;
  70. /** \brief Style attribute. */
  71. typedef struct pmh_style_attribute
  72. {
  73. pmh_element_type lang_element_type; /**< The Markdown language element this
  74. style applies to */
  75. pmh_attr_type type; /**< The type of the attribute */
  76. char *name; /**< The name of the attribute (if type
  77. is pmh_attr_type_other, you can
  78. use this value to determine what
  79. the attribute is) */
  80. pmh_attr_value *value; /**< The value of the attribute */
  81. struct pmh_style_attribute *next; /**< Next attribute in linked list */
  82. } pmh_style_attribute;
  83. /** \brief Collection of styles. */
  84. typedef struct
  85. {
  86. /** Styles that apply to the editor in general */
  87. pmh_style_attribute *editor_styles;
  88. /** Styles that apply to the line in the editor where the caret (insertion
  89. point) resides */
  90. pmh_style_attribute *editor_current_line_styles;
  91. /** Styles that apply to the range of selected text in the editor */
  92. pmh_style_attribute *editor_selection_styles;
  93. /** Styles that apply to specific Markdown language elements */
  94. pmh_style_attribute **element_styles;
  95. } pmh_style_collection;
  96. /**
  97. * \brief Parse stylesheet string, return style collection
  98. *
  99. * \param[in] input The stylesheet string to parse.
  100. * \param[in] error_callback Callback function to be called when errors
  101. * occur during parsing. The first argument
  102. * to the callback function is the error
  103. * message and the second one the line number
  104. * in the original input where the error
  105. * occurred. The last argument will always
  106. * get the value you pass in for the
  107. * error_callback_context argument to this
  108. * function.
  109. * Pass in NULL to suppress error reporting.
  110. * \param[in] error_callback_context Arbitrary context pointer for the error
  111. * callback function; will be passed in as
  112. * the last argument to error_callback.
  113. *
  114. * \return A pmh_style_collection. You must pass this value to
  115. * pmh_free_style_collection() when it's not needed anymore.
  116. */
  117. pmh_style_collection *pmh_parse_styles(char *input,
  118. void(*error_callback)(char*,int,void*),
  119. void *error_callback_context);
  120. /**
  121. * \brief Free a pmh_style_collection.
  122. *
  123. * Frees a pmh_style_collection value returned by pmh_parse_styles().
  124. *
  125. * \param[in] collection The collection to free.
  126. */
  127. void pmh_free_style_collection(pmh_style_collection *collection);
  128. char *pmh_attr_name_from_type(pmh_attr_type type);
  129. pmh_attr_type pmh_attr_type_from_name(char *name);