KDP(電子出版)のメモ 急急如律令

Amazon Kindleダイレクト・パブリッシングでの電子出版や電子書籍の作成販売について、文章やイラストの作成や編集方法について書いています。

AozoraEpub3のアンドロイド移植

前に作ろうとしてから3年ぐらい経っている感じ。imageio部分を削るのとvelocityを使うところまではできた。

99nyorituryo.hatenablog.com

99nyorituryo.hatenablog.com

MainActivity以外からリソースへのアクセスがしたい。

stackoverflow.com

String jarPathからContext jarPath

AozoraEpub3ではmainからjarファイルが実行しているパスを各クラスに渡して、テンプレートファイルや設定ファイルを読み込んでいる。アンドロイドでは、リソースファイルはresフォルダーやassetsフォルダーで管理している。

 jarpathからresやassetsに書き換える必要がある。

resやassetsにアクセスするのは。

developer.android.com

String jarPath ="";
        AozoraGaijiConverter gaijiconverter;
            gaijiconverter = new AozoraGaijiConverter(jarPath);

         gaijiconverter = new AozoraGaijiConverter(this);

    public AozoraGaijiConverter(String jarPath) throws IOException
    {
        //初期化
        //ファイルチェック取得 IVS優先
        this.loadChukiFile(new File(jarPath+"chuki_ivs.txt"), chukiUtfMap);
        this.loadChukiFile(new File(jarPath+"chuki_utf.txt"), chukiUtfMap);
        this.loadChukiFile(new File(jarPath+"chuki_alt.txt"), chukiAltMap);
    }

    public AozoraGaijiConverter(Context context) throws IOException
    {
        //初期化
        //ファイルチェック取得 IVS優先

        this.loadChukiFile(context.getAssets().open("chuki_ivs.txt"), chukiUtfMap);
        this.loadChukiFile(context.getAssets().open("chuki_utf.txt"), chukiUtfMap);
        this.loadChukiFile(context.getAssets().open("chuki_alt.txt"), chukiAltMap);
    }

chuki_alt.txt chuki_ivs.txt chuki_latin.txt chuki_tag.txt chuki_tag_suf.txt chuki_utf.txt

     this.loadChukiFile(context.getAssets().open("chuki_ivs.txt"), chukiUtfMap);
        this.loadChukiFile(context.getAssets().open("chuki_utf.txt"), chukiUtfMap);
        this.loadChukiFile(context.getAssets().open("chuki_alt.txt"), chukiAltMap);

BufferedReader src = new BufferedReader(new InputStreamReader(context.getAssets().open("chuki_tag.txt"), StandardCharsets.UTF_8));
src = new BufferedReader(new InputStreamReader(context.getAssets().open("chuki_tag_suf.txt"), StandardCharsets.UTF_8));
src = new BufferedReader(new InputStreamReader(context.getAssets().open("replace.txt"), StandardCharsets.UTF_8));
    Epub3Writer writer = new Epub3Writer(this);

contextを渡すようにする

inputstreamからstring変換 stackoverflow.com

Velocity.mergeTemplateではファイルパスからテンプレートファイルを読み込むが、context.getAssets().openではinputstreamになるので Velocity.evaluateを使って、ファイルパスではなくテンプレートファイルのstringを渡すように変える。

Velocity.mergeTemplate(templatePath+OPS_PATH+CSS_PATH+TEXT_CSS_VM, "UTF-8", velocityContext, bw);


String template = IOUtils.toString(context.getAssets().open(templatePath+OPS_PATH+CSS_PATH+TEXT_CSS_VM), StandardCharsets.UTF_8);
Velocity.evaluate(velocityContext, bw, null, template);

String template = inputStreemToString(context.getAssets().open(templatePath+OPS_PATH+CSS_PATH+TEXT_CSS_VM), StandardCharsets.UTF_8);
Velocity.evaluate(velocityContext, bw, null, template);

            //Velocity.mergeTemplate(vmFilePath, "UTF-8", velocityContext, bw);
            String template = inputStreemToString(context.getAssets().open(vmFilePath), StandardCharsets.UTF_8);
            Velocity.evaluate(velocityContext, bw, null, template);

                //Velocity.mergeTemplate(templatePath+OPS_PATH+XHTML_PATH+COVER_VM, "UTF-8", velocityContext, bw);
                String template = inputStreemToString(context.getAssets().open(templatePath+OPS_PATH+XHTML_PATH+COVER_VM), StandardCharsets.UTF_8);
                Velocity.evaluate(velocityContext, bw, null, template);

        //Velocity.mergeTemplate(templatePath+OPS_PATH+PACKAGE_VM, "UTF-8", velocityContext, bw);
        String template = inputStreemToString(context.getAssets().open(templatePath+OPS_PATH+PACKAGE_VM), StandardCharsets.UTF_8);
        Velocity.evaluate(velocityContext, bw, null, template);

        //Velocity.mergeTemplate(templatePath+OPS_PATH+XHTML_PATH+XHTML_NAV_VM, "UTF-8", velocityContext, bw);
        template = inputStreemToString(context.getAssets().open(templatePath+OPS_PATH+XHTML_PATH+XHTML_NAV_VM), StandardCharsets.UTF_8);
        Velocity.evaluate(velocityContext, bw, null, template);

        //Velocity.mergeTemplate(templatePath+OPS_PATH+TOC_VM, "UTF-8", velocityContext, bw);
        template = inputStreemToString(context.getAssets().open(templatePath+OPS_PATH+TOC_VM), StandardCharsets.UTF_8);
        Velocity.evaluate(velocityContext, bw, null, template);

        //Velocity.mergeTemplate(this.templatePath+OPS_PATH+XHTML_PATH+XHTML_HEADER_VM, "UTF-8", velocityContext, bw);
        String template = inputStreemToString(context.getAssets().open(this.templatePath+OPS_PATH+XHTML_PATH+XHTML_HEADER_VM), StandardCharsets.UTF_8);
        Velocity.evaluate(velocityContext, bw, null, template);

        //Velocity.mergeTemplate(this.templatePath+OPS_PATH+XHTML_PATH+XHTML_FOOTER_VM, "UTF-8", velocityContext, bw);
        String template = inputStreemToString(context.getAssets().open(this.templatePath+OPS_PATH+XHTML_PATH+XHTML_FOOTER_VM), StandardCharsets.UTF_8);
        Velocity.evaluate(velocityContext, bw, null, template);

    public Epub3ImageWriter(Context context)
    {
        super(context);
    }

gist.github.com

androidでapche velocityは動くのだろうか 99nyorituryo.hatenablog.com View Binding codeforfun.jp androidでファイルの保存 99nyorituryo.hatenablog.com Storage Access Framework でドキュメントを保存する akira-watson.com

ja.getdocs.org

99nyorituryo.hatenablog.com