~alcinnz/rhapsode

c07c86488309ce17d3ee024e4fe00cd555919a66 — Adrian Cochrane 4 years ago d47ff00
Allow disabling of URL rewriting for SSML output.
3 files changed, 19 insertions(+), 16 deletions(-)

M src/CExports.hs
M src/Render.hs
M src/main.c
M src/CExports.hs => src/CExports.hs +4 -4
@@ 33,12 33,12 @@ import Foreign.Marshal.Array
--pair a b = (a, b)

-- FIXME: Segfaults, was intended for the sake of easy concurrency.
foreign export ccall c_docLinksAndRendering :: StablePtr Session -> StablePtr Page -> IO (CArray CString)
foreign export ccall c_docLinksAndRendering :: StablePtr Session -> StablePtr Page -> Bool -> IO (CArray CString)

c_docLinksAndRendering c_session c_page = do
c_docLinksAndRendering c_session c_page rewriteUrls = do
    c_links <- c_extractLinks c_page
    ssml <- c_renderDoc c_session c_page
    -- (c_links, ssml) <- c_extractLinks c_page `concurrently` c_renderDoc c_session c_page
    ssml <- c_renderDoc c_session c_page rewriteUrls
    -- (c_links, ssml) <- c_extractLinks c_page `concurrently` c_renderDoc c_session c_page rewriteUrls
    nil <- newCString ""
    links <- peekArray0 nil c_links
    newArray0 nil (ssml : links)

M src/Render.hs => src/Render.hs +12 -9
@@ 211,19 211,22 @@ applyCounters root = root {
--------
---- C API
--------
foreign export ccall c_renderDoc :: StablePtr Session -> StablePtr Page -> IO CString -- Hard to C bindings without IO
foreign export ccall c_renderDoc :: StablePtr Session -> StablePtr Page -> Bool -> IO CString -- Hard to C bindings without IO

c_renderDoc c_session c_page = do
c_renderDoc c_session c_page rewriteURLs = do
    session <- deRefStablePtr c_session
    page <- deRefStablePtr c_page
    css' <- retreiveStyles session $ css page
    assets <- downloadAssets session [
            -- FIXME couldn't find MIMEtypes for all the audio formats 
            "audio/vnd.wav", "audio/x-aiff", "audio/basic", "audio/8svx", "audio/x-8svx",
            "audio/x-voc", "application/x-pagerecall", "audio/x-caf", "audio/flac",
            "audio/x-oga", "application/x-ogg", "application/ogg"
        ] $ resolve' (StyleAssets ["cue-before", "cue-after", "cue"] []) css'
    let qCSS = resolve' Style.queryableStyleSheet css'
    qCSS <- if rewriteURLs then do
        assets <- downloadAssets session [
                -- FIXME couldn't find MIMEtypes for all the audio formats 
                "audio/vnd.wav", "audio/x-aiff", "audio/basic", "audio/8svx", "audio/x-8svx",
                "audio/x-voc", "application/x-pagerecall", "audio/x-caf", "audio/flac",
                "audio/x-oga", "application/x-ogg", "application/ogg"
            ] $ resolve' (StyleAssets ["cue-before", "cue-after", "cue"] []) css'
        let URIRewriter _ qCSS' =  resolve' (URIRewriter (M.fromList assets) Style.queryableStyleSheet) css'
        return qCSS'
        else return $ resolve' Style.queryableStyleSheet css'
    let ssml = renderDoc qCSS $ XML.documentRoot $ html page
    B.toStrict ssml `useAsCString` \cstr -> do
        str <- peekCString cstr

M src/main.c => src/main.c +3 -3
@@ 19,9 19,9 @@ void *c_fetchURL(struct session*, char*, struct page*, char*);
//struct page **c_fetchURLs(struct session*, struct page*, char**); // FIXME segfaults.
void c_freePage(struct page*);

char *c_renderDoc(struct session*, struct page*);
char *c_renderDoc(struct session*, struct page*, _Bool);
char **c_extractLinks(struct page*);
char **c_docLinksAndRendering(struct session*, struct page*); // FIXME segfaults.
char **c_docLinksAndRendering(struct session*, struct page*, _Bool); // FIXME segfaults.

/* espeak-ng integration. Based on the espeak-ng command source code. */
SNDFILE *fd_wav = NULL;


@@ 172,7 172,7 @@ int main(int argc, char **argv) {
        else printf("%s\n", argv[i]);

        struct page *page = c_fetchURL(session, mimes, referer, argv[i]);
        char *ssml = c_renderDoc(session, page);
        char *ssml = c_renderDoc(session, page, use_espeak);
        char **links = c_extractLinks(page);

        if (fd_ssml != NULL) fprintf(fd_ssml, "%s\n", ssml);