1
This commit is contained in:
Alex Yatskov 2016-11-20 18:54:57 -08:00
parent 7d049e1f0a
commit 5bde395bb2
3 changed files with 119 additions and 90 deletions

195
hooks.c
View File

@ -53,46 +53,53 @@
* Local functions * Local functions
*/ */
HOOK_TAGGER(begin_candidate); /* EB_HOOK_BEGIN_CANDIDATE */ HOOK_TAGGER(begin_candidate); /* EB_HOOK_BEGIN_CANDIDATE */
HOOK_TAGGER(begin_color_bmp); /* EB_HOOK_BEGIN_COLOR_BMP */ HOOK_TAGGER(begin_clickable_area); /* EB_HOOK_BEGIN_CLICKABLE_AREA */
HOOK_TAGGER(begin_color_jpeg); /* EB_HOOK_BEGIN_COLOR_JPEG */ HOOK_TAGGER(begin_color_bmp); /* EB_HOOK_BEGIN_COLOR_BMP */
HOOK_TAGGER(begin_decoration); /* EB_HOOK_BEGIN_DECORATION */ HOOK_TAGGER(begin_color_jpeg); /* EB_HOOK_BEGIN_COLOR_JPEG */
HOOK_TAGGER(begin_emphasis); /* EB_HOOK_BEGIN_EMPHASIS */ HOOK_TAGGER(begin_decoration); /* EB_HOOK_BEGIN_DECORATION */
HOOK_TAGGER(begin_gray_graphic); /* EB_HOOK_BEGIN_GRAY_GRAPHIC */ HOOK_TAGGER(begin_ebxac_gaiji); /* EB_HOOK_BEGIN_EBXAC_GAIJI */
HOOK_TAGGER(begin_in_color_bmp); /* EB_HOOK_BEGIN_IN_COLOR_BMP */ HOOK_TAGGER(begin_emphasis); /* EB_HOOK_BEGIN_EMPHASIS */
HOOK_TAGGER(begin_in_color_jpeg); /* EB_HOOK_BEGIN_IN_COLOR_JPEG */ HOOK_TAGGER(begin_graphic_reference); /* EB_HOOK_BEGIN_GRAPHIC_REFERENCE */
HOOK_TAGGER(begin_keyword); /* EB_HOOK_BEGIN_KEYWORD */ HOOK_TAGGER(begin_gray_graphic); /* EB_HOOK_BEGIN_GRAY_GRAPHIC */
HOOK_TAGGER(begin_mono_graphic); /* EB_HOOK_BEGIN_MONO_GRAPHIC */ HOOK_TAGGER(begin_image_page); /* EB_HOOK_BEGIN_IMAGE_PAGE */
HOOK_TAGGER(begin_mpeg); /* EB_HOOK_BEGIN_MPEG */ HOOK_TAGGER(begin_in_color_bmp); /* EB_HOOK_BEGIN_IN_COLOR_BMP */
HOOK_TAGGER(begin_narrow); /* EB_HOOK_BEGIN_NARROW */ HOOK_TAGGER(begin_in_color_jpeg); /* EB_HOOK_BEGIN_IN_COLOR_JPEG */
HOOK_TAGGER(begin_no_newline); /* EB_HOOK_BEGIN_NO_NEWLINE */ HOOK_TAGGER(begin_keyword); /* EB_HOOK_BEGIN_KEYWORD */
HOOK_TAGGER(begin_reference); /* EB_HOOK_BEGIN_REFERENCE */ HOOK_TAGGER(begin_mono_graphic); /* EB_HOOK_BEGIN_MONO_GRAPHIC */
HOOK_TAGGER(begin_subscript); /* EB_HOOK_BEGIN_SUBSCRIPT */ HOOK_TAGGER(begin_mpeg); /* EB_HOOK_BEGIN_MPEG */
HOOK_TAGGER(begin_superscript); /* EB_HOOK_BEGIN_SUPERSCRIPT */ HOOK_TAGGER(begin_narrow); /* EB_HOOK_BEGIN_NARROW */
HOOK_TAGGER(begin_wave); /* EB_HOOK_BEGIN_WAVE */ HOOK_TAGGER(begin_no_newline); /* EB_HOOK_BEGIN_NO_NEWLINE */
HOOK_TAGGER(end_candidate_group); /* EB_HOOK_END_CANDIDATE_GROUP */ HOOK_TAGGER(begin_reference); /* EB_HOOK_BEGIN_REFERENCE */
HOOK_TAGGER(end_candidate_leaf); /* EB_HOOK_END_CANDIDATE_LEAF */ HOOK_TAGGER(begin_subscript); /* EB_HOOK_BEGIN_SUBSCRIPT */
HOOK_TAGGER(end_color_graphic); /* EB_HOOK_END_COLOR_GRAPHIC */ HOOK_TAGGER(begin_superscript); /* EB_HOOK_BEGIN_SUPERSCRIPT */
HOOK_TAGGER(end_decoration); /* EB_HOOK_END_DECORATION */ HOOK_TAGGER(begin_unicode); /* EB_HOOK_BEGIN_UNICODE */
HOOK_TAGGER(end_emphasis); /* EB_HOOK_END_EMPHASIS */ HOOK_TAGGER(begin_wave); /* EB_HOOK_BEGIN_WAVE */
HOOK_TAGGER(end_gray_graphic); /* EB_HOOK_END_GRAY_GRAPHIC */ HOOK_TAGGER(end_candidate_group); /* EB_HOOK_END_CANDIDATE_GROUP */
HOOK_TAGGER(end_in_color_graphic); /* EB_HOOK_SET_END_IN_COLOR_GRAPHIC */ HOOK_TAGGER(end_candidate_leaf); /* EB_HOOK_END_CANDIDATE_LEAF */
HOOK_TAGGER(end_keyword); /* EB_HOOK_END_KEYWORD */ HOOK_TAGGER(end_clickable_area); /* EB_HOOK_END_CLICKABLE_AREA */
HOOK_TAGGER(end_mono_graphic); /* EB_HOOK_END_MONO_GRAPHIC */ HOOK_TAGGER(end_color_graphic); /* EB_HOOK_END_COLOR_GRAPHIC */
HOOK_TAGGER(end_mpeg); /* EB_HOOK_END_MPEG */ HOOK_TAGGER(end_decoration); /* EB_HOOK_END_DECORATION */
HOOK_TAGGER(end_narrow); /* EB_HOOK_END_NARROW */ HOOK_TAGGER(end_ebxac_gaiji); /* EB_HOOK_END_EBXAC_GAIJI */
HOOK_TAGGER(end_no_newline); /* EB_HOOK_END_NO_NEWLINE */ HOOK_TAGGER(end_emphasis); /* EB_HOOK_END_EMPHASIS */
HOOK_TAGGER(end_reference); /* EB_HOOK_END_REFERENCE */ HOOK_TAGGER(end_graphic_reference); /* EB_HOOK_END_GRAPHIC_REFERENCE */
HOOK_TAGGER(end_subscript); /* EB_HOOK_END_SUBSCRIPT */ HOOK_TAGGER(end_gray_graphic); /* EB_HOOK_END_GRAY_GRAPHIC */
HOOK_TAGGER(end_superscript); /* EB_HOOK_END_SUPERSCRIPT */ HOOK_TAGGER(end_image_page); /* EB_HOOK_END_IMAGE_PAGE */
HOOK_TAGGER(end_wave); /* EB_HOOK_END_WAVE */ HOOK_TAGGER(end_in_color_graphic); /* EB_HOOK_END_IN_COLOR_GRAPHIC */
HOOK_TAGGER(gb2312); /* EB_HOOK_GB2312 */ HOOK_TAGGER(end_keyword); /* EB_HOOK_END_KEYWORD */
HOOK_TAGGER(iso8859_1); /* EB_HOOK_ISO8859_1 */ HOOK_TAGGER(end_mono_graphic); /* EB_HOOK_END_MONO_GRAPHIC */
HOOK_TAGGER(narrow_jisx0208); /* EB_HOOK_NARROW_JISX0208 */ HOOK_TAGGER(end_mpeg); /* EB_HOOK_END_MPEG */
HOOK_TAGGER(newline); /* EB_HOOK_NEWLINE */ HOOK_TAGGER(end_narrow); /* EB_HOOK_END_NARROW */
HOOK_TAGGER(set_indent); /* EB_HOOK_SET_INDENT */ HOOK_TAGGER(end_no_newline); /* EB_HOOK_END_NO_NEWLINE */
HOOK_TAGGER(wide_jisx0208); /* EB_HOOK_WIDE_JISX0208 */ HOOK_TAGGER(end_reference); /* EB_HOOK_END_REFERENCE */
HOOK_TAGGER(null); /* EB_HOOK_NULL */ HOOK_TAGGER(end_subscript); /* EB_HOOK_END_SUBSCRIPT */
HOOK_TAGGER(end_superscript); /* EB_HOOK_END_SUPERSCRIPT */
HOOK_TAGGER(end_unicode); /* EB_HOOK_END_UNICODE */
HOOK_TAGGER(end_wave); /* EB_HOOK_END_WAVE */
HOOK_TAGGER(graphic_reference); /* EB_HOOK_GRAPHIC_REFERENCE */
HOOK_TAGGER(newline); /* EB_HOOK_NEWLINE */
HOOK_TAGGER(null); /* EB_HOOK_NULL */
HOOK_TAGGER(set_indent); /* EB_HOOK_SET_INDENT */
static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */
EB_Book* book, EB_Book* book,
@ -136,57 +143,73 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */
* Local data * Local data
*/ */
static const EB_Hook s_hooks[] = { static const EB_Hook s_hooks_markup[] = {
{ EB_HOOK_BEGIN_CANDIDATE, HOOK_FUNC_NAME(begin_candidate) }, { EB_HOOK_BEGIN_CANDIDATE, HOOK_FUNC_NAME(begin_candidate) },
{ EB_HOOK_BEGIN_COLOR_BMP, HOOK_FUNC_NAME(begin_color_bmp) }, { EB_HOOK_BEGIN_CLICKABLE_AREA, HOOK_FUNC_NAME(begin_clickable_area) },
{ EB_HOOK_BEGIN_COLOR_JPEG, HOOK_FUNC_NAME(begin_color_jpeg) }, { EB_HOOK_BEGIN_COLOR_BMP, HOOK_FUNC_NAME(begin_color_bmp) },
{ EB_HOOK_BEGIN_DECORATION, HOOK_FUNC_NAME(begin_decoration) }, { EB_HOOK_BEGIN_COLOR_JPEG, HOOK_FUNC_NAME(begin_color_jpeg) },
{ EB_HOOK_BEGIN_EMPHASIS, HOOK_FUNC_NAME(begin_emphasis) }, { EB_HOOK_BEGIN_DECORATION, HOOK_FUNC_NAME(begin_decoration) },
{ EB_HOOK_BEGIN_GRAY_GRAPHIC, HOOK_FUNC_NAME(begin_gray_graphic) }, { EB_HOOK_BEGIN_EBXAC_GAIJI, HOOK_FUNC_NAME(begin_ebxac_gaiji) },
{ EB_HOOK_BEGIN_IN_COLOR_BMP, HOOK_FUNC_NAME(begin_in_color_bmp) }, { EB_HOOK_BEGIN_EMPHASIS, HOOK_FUNC_NAME(begin_emphasis) },
{ EB_HOOK_BEGIN_IN_COLOR_JPEG, HOOK_FUNC_NAME(begin_in_color_jpeg) }, { EB_HOOK_BEGIN_GRAPHIC_REFERENCE, HOOK_FUNC_NAME(begin_graphic_reference) },
{ EB_HOOK_BEGIN_KEYWORD, HOOK_FUNC_NAME(begin_keyword) }, { EB_HOOK_BEGIN_GRAY_GRAPHIC, HOOK_FUNC_NAME(begin_gray_graphic) },
{ EB_HOOK_BEGIN_MONO_GRAPHIC, HOOK_FUNC_NAME(begin_mono_graphic) }, { EB_HOOK_BEGIN_IMAGE_PAGE, HOOK_FUNC_NAME(begin_image_page) },
{ EB_HOOK_BEGIN_MPEG, HOOK_FUNC_NAME(begin_mpeg) }, { EB_HOOK_BEGIN_IN_COLOR_BMP, HOOK_FUNC_NAME(begin_in_color_bmp) },
{ EB_HOOK_BEGIN_NARROW, HOOK_FUNC_NAME(begin_narrow) }, { EB_HOOK_BEGIN_IN_COLOR_JPEG, HOOK_FUNC_NAME(begin_in_color_jpeg) },
{ EB_HOOK_BEGIN_NO_NEWLINE, HOOK_FUNC_NAME(begin_no_newline) }, { EB_HOOK_BEGIN_KEYWORD, HOOK_FUNC_NAME(begin_keyword) },
{ EB_HOOK_BEGIN_REFERENCE, HOOK_FUNC_NAME(begin_reference) }, { EB_HOOK_BEGIN_MONO_GRAPHIC, HOOK_FUNC_NAME(begin_mono_graphic) },
{ EB_HOOK_BEGIN_SUBSCRIPT, HOOK_FUNC_NAME(begin_subscript) }, { EB_HOOK_BEGIN_MPEG, HOOK_FUNC_NAME(begin_mpeg) },
{ EB_HOOK_BEGIN_SUPERSCRIPT, HOOK_FUNC_NAME(begin_superscript) }, { EB_HOOK_BEGIN_NARROW, HOOK_FUNC_NAME(begin_narrow) },
{ EB_HOOK_BEGIN_WAVE, HOOK_FUNC_NAME(begin_wave) }, { EB_HOOK_BEGIN_NO_NEWLINE, HOOK_FUNC_NAME(begin_no_newline) },
{ EB_HOOK_END_CANDIDATE_GROUP, HOOK_FUNC_NAME(end_candidate_group) }, { EB_HOOK_BEGIN_REFERENCE, HOOK_FUNC_NAME(begin_reference) },
{ EB_HOOK_END_CANDIDATE_LEAF, HOOK_FUNC_NAME(end_candidate_leaf) }, { EB_HOOK_BEGIN_SUBSCRIPT, HOOK_FUNC_NAME(begin_subscript) },
{ EB_HOOK_END_COLOR_GRAPHIC, HOOK_FUNC_NAME(end_color_graphic) }, { EB_HOOK_BEGIN_SUPERSCRIPT, HOOK_FUNC_NAME(begin_superscript) },
{ EB_HOOK_END_DECORATION, HOOK_FUNC_NAME(end_decoration) }, { EB_HOOK_BEGIN_UNICODE, HOOK_FUNC_NAME(begin_unicode) },
{ EB_HOOK_END_EMPHASIS, HOOK_FUNC_NAME(end_emphasis) }, { EB_HOOK_BEGIN_WAVE, HOOK_FUNC_NAME(begin_wave) },
{ EB_HOOK_END_GRAY_GRAPHIC, HOOK_FUNC_NAME(end_gray_graphic) }, { EB_HOOK_END_CANDIDATE_GROUP, HOOK_FUNC_NAME(end_candidate_group) },
{ EB_HOOK_END_IN_COLOR_GRAPHIC, HOOK_FUNC_NAME(end_in_color_graphic) }, { EB_HOOK_END_CANDIDATE_LEAF, HOOK_FUNC_NAME(end_candidate_leaf) },
{ EB_HOOK_END_KEYWORD, HOOK_FUNC_NAME(end_keyword) }, { EB_HOOK_END_CLICKABLE_AREA, HOOK_FUNC_NAME(end_clickable_area) },
{ EB_HOOK_END_MONO_GRAPHIC, HOOK_FUNC_NAME(end_mono_graphic) }, { EB_HOOK_END_COLOR_GRAPHIC, HOOK_FUNC_NAME(end_color_graphic) },
{ EB_HOOK_END_MPEG, HOOK_FUNC_NAME(end_mpeg) }, { EB_HOOK_END_DECORATION, HOOK_FUNC_NAME(end_decoration) },
{ EB_HOOK_END_NARROW, HOOK_FUNC_NAME(end_narrow) }, { EB_HOOK_END_EBXAC_GAIJI, HOOK_FUNC_NAME(end_ebxac_gaiji) },
{ EB_HOOK_END_NO_NEWLINE, HOOK_FUNC_NAME(end_no_newline) }, { EB_HOOK_END_EMPHASIS, HOOK_FUNC_NAME(end_emphasis) },
{ EB_HOOK_END_REFERENCE, HOOK_FUNC_NAME(end_reference) }, { EB_HOOK_END_GRAPHIC_REFERENCE, HOOK_FUNC_NAME(end_graphic_reference) },
{ EB_HOOK_END_SUBSCRIPT, HOOK_FUNC_NAME(end_subscript) }, { EB_HOOK_END_GRAY_GRAPHIC, HOOK_FUNC_NAME(end_gray_graphic) },
{ EB_HOOK_END_SUPERSCRIPT, HOOK_FUNC_NAME(end_superscript) }, { EB_HOOK_END_IMAGE_PAGE, HOOK_FUNC_NAME(end_image_page) },
{ EB_HOOK_END_WAVE, HOOK_FUNC_NAME(end_wave) }, { EB_HOOK_END_IN_COLOR_GRAPHIC, HOOK_FUNC_NAME(end_in_color_graphic) },
{ EB_HOOK_GB2312, HOOK_FUNC_NAME(gb2312) }, { EB_HOOK_END_KEYWORD, HOOK_FUNC_NAME(end_keyword) },
{ EB_HOOK_ISO8859_1, HOOK_FUNC_NAME(iso8859_1) }, { EB_HOOK_END_MONO_GRAPHIC, HOOK_FUNC_NAME(end_mono_graphic) },
{ EB_HOOK_NARROW_FONT, HOOK_FUNC_NAME(narrow_font) }, { EB_HOOK_END_MPEG, HOOK_FUNC_NAME(end_mpeg) },
/* { EB_HOOK_NARROW_JISX0208, HOOK_FUNC_NAME(narrow_jisx0208) }, */ { EB_HOOK_END_NARROW, HOOK_FUNC_NAME(end_narrow) },
{ EB_HOOK_NEWLINE, HOOK_FUNC_NAME(newline) }, { EB_HOOK_END_NO_NEWLINE, HOOK_FUNC_NAME(end_no_newline) },
{ EB_HOOK_SET_INDENT, HOOK_FUNC_NAME(set_indent) }, { EB_HOOK_END_REFERENCE, HOOK_FUNC_NAME(end_reference) },
{ EB_HOOK_WIDE_FONT, HOOK_FUNC_NAME(wide_font) }, { EB_HOOK_END_SUBSCRIPT, HOOK_FUNC_NAME(end_subscript) },
/* { EB_HOOK_WIDE_JISX0208, HOOK_FUNC_NAME(wide_jisx0208) }, */ { EB_HOOK_END_SUPERSCRIPT, HOOK_FUNC_NAME(end_superscript) },
{ EB_HOOK_NULL, HOOK_FUNC_NAME(null) }, { EB_HOOK_END_UNICODE, HOOK_FUNC_NAME(end_unicode) },
{ EB_HOOK_END_WAVE, HOOK_FUNC_NAME(end_wave) },
{ EB_HOOK_GRAPHIC_REFERENCE, HOOK_FUNC_NAME(graphic_reference) },
{ EB_HOOK_NEWLINE, HOOK_FUNC_NAME(newline) },
{ EB_HOOK_NULL, HOOK_FUNC_NAME(null) },
{ EB_HOOK_SET_INDENT, HOOK_FUNC_NAME(set_indent) },
};
static const EB_Hook s_hooks_basic[] = {
{ EB_HOOK_WIDE_FONT, HOOK_FUNC_NAME(wide_font) },
{ EB_HOOK_NARROW_FONT, HOOK_FUNC_NAME(narrow_font) },
}; };
/* /*
* Exported functions * Exported functions
*/ */
void hooks_install(EB_Hookset* hookset) { void hooks_install(EB_Hookset* hookset, bool markup) {
for (unsigned i = 0; i < ARRSIZE(s_hooks); ++i) { for (unsigned i = 0; i < ARRSIZE(s_hooks_basic); ++i) {
eb_set_hook(hookset, s_hooks + i); eb_set_hook(hookset, s_hooks_basic + i);
}
if (markup) {
for (unsigned i = 0; i < ARRSIZE(s_hooks_markup); ++i) {
eb_set_hook(hookset, s_hooks_markup + i);
}
} }
} }

View File

@ -19,12 +19,14 @@
#ifndef HOOKS_H #ifndef HOOKS_H
#define HOOKS_H #define HOOKS_H
#include <stdbool.h>
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
/* /*
* Exported functions * Exported functions
*/ */
void hooks_install(EB_Hookset* hookset); void hooks_install(EB_Hookset* hookset, bool markup);
#endif /* HOOKS_H */ #endif /* HOOKS_H */

10
main.c
View File

@ -93,7 +93,7 @@ static void export_subbook(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset*
} }
} }
static void export_book(Book* book, const char path[]) { static void export_book(Book* book, const char path[], bool markup) {
do { do {
EB_Error_Code error; EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) { if ((error = eb_initialize_library()) != EB_SUCCESS) {
@ -106,7 +106,7 @@ static void export_book(Book* book, const char path[]) {
EB_Hookset eb_hookset; EB_Hookset eb_hookset;
eb_initialize_hookset(&eb_hookset); eb_initialize_hookset(&eb_hookset);
hooks_install(&eb_hookset); hooks_install(&eb_hookset, markup);
if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) { if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) {
fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error)); fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error));
@ -181,6 +181,7 @@ static void export_book(Book* book, const char path[]) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool pretty_print = false; bool pretty_print = false;
bool markup = false;
char opt = 0; char opt = 0;
while ((opt = getopt(argc, argv, "p")) != -1) { while ((opt = getopt(argc, argv, "p")) != -1) {
@ -188,6 +189,9 @@ int main(int argc, char *argv[]) {
case 'p': case 'p':
pretty_print = true; pretty_print = true;
break; break;
case 'm':
markup = true;
break;
default: default:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
@ -200,7 +204,7 @@ int main(int argc, char *argv[]) {
} }
Book book = {}; Book book = {};
export_book(&book, argv[optind]); export_book(&book, argv[optind], markup);
book_dump(&book, pretty_print, stdout); book_dump(&book, pretty_print, stdout);
book_free(&book); book_free(&book);