/* * Return a table like this: * { * { * page=12, * xpointer = "/body/DocFragment[11].0", * depth=1, * title="chapter1" * }, * { * page=54, * xpointer = "/body/DocFragment[13].0", * depth=1, * title="chapter2" * }, * } * */ static int getTableOfContent(lua_State *L) { CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); LVTocItem * toc = doc->text_view->getToc(); int count = 1; lua_newtable(L); walkTableOfContent(L, toc, &count); return 1; }
static int walkTableOfContent(lua_State *L, miniexp_t r, int *count, int depth) { depth++; miniexp_t lista = miniexp_cdr(r); // go inside bookmars in the list int length = miniexp_length(r); int counter = 0; const char* page_name; int page_number; while(counter < length-1) { lua_pushnumber(L, *count); lua_newtable(L); lua_pushstring(L, "page"); page_name = miniexp_to_str(miniexp_car(miniexp_cdr(miniexp_nth(counter, lista)))); if(page_name != NULL && page_name[0] == '#') { errno = 0; page_number = strtol(page_name + 1, NULL, 10); if(!errno) { lua_pushnumber(L, page_number); } else { /* we can not parse this as a number, TODO: parse page names */ lua_pushnumber(L, -1); } } else { /* something we did not expect here */ lua_pushnumber(L, -1); } lua_settable(L, -3); lua_pushstring(L, "depth"); lua_pushnumber(L, depth); lua_settable(L, -3); lua_pushstring(L, "title"); lua_pushstring(L, miniexp_to_str(miniexp_car(miniexp_nth(counter, lista)))); lua_settable(L, -3); lua_settable(L, -3); (*count)++; if (miniexp_length(miniexp_cdr(miniexp_nth(counter, lista))) > 1) { walkTableOfContent(L, miniexp_cdr(miniexp_nth(counter, lista)), count, depth); } counter++; } return 0; }
static int getTableOfContent(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); miniexp_t r; int count = 1; while ((r=ddjvu_document_get_outline(doc->doc_ref))==miniexp_dummy) handle(L, doc->context, True); //printf("lista: %s\n", miniexp_to_str(miniexp_car(miniexp_nth(1, miniexp_cdr(r))))); lua_newtable(L); walkTableOfContent(L, r, &count, 0); return 1; }
/* * helper function for getTableOfContent() */ static int walkTableOfContent(lua_State *L, LVTocItem *toc, int *count) { LVTocItem *toc_tmp = NULL; int i = 0, nr_child = toc->getChildCount(); for (i = 0; i < nr_child; i++) { toc_tmp = toc->getChild(i); lua_pushnumber(L, (*count)++); /* set subtable, Toc entry */ lua_newtable(L); lua_pushstring(L, "page"); lua_pushnumber(L, toc_tmp->getPage()+1); lua_settable(L, -3); lua_pushstring(L, "xpointer"); lua_pushstring(L, UnicodeToLocal( toc_tmp->getXPointer().toString()).c_str() ); lua_settable(L, -3); lua_pushstring(L, "depth"); lua_pushnumber(L, toc_tmp->getLevel()); lua_settable(L, -3); lua_pushstring(L, "title"); lua_pushstring(L, UnicodeToLocal(toc_tmp->getName()).c_str()); lua_settable(L, -3); /* set Toc entry to Toc table */ lua_settable(L, -3); if (toc_tmp->getChildCount() > 0) { walkTableOfContent(L, toc_tmp, count); } } return 0; }