Exemple #1
0
        public void haveEmbeddedItem(String src, String dir, ManifestFile mf, HashSet <String> not_found)
        {
            String       comb = this.purgeIntermediatePaths(dir + src);
            ManifestFile matching_file;

            if (this._DictItemsByPath.TryGetValue(comb, out matching_file))
            {
                if (mf.Dependants.Contains(matching_file))
                {
                    ;
                }
                else if (mf == matching_file)
                {
                    ;
                }
                else
                {
                    mf.Dependants.Add(matching_file);
                    matching_file.addReferenceMax();
                    if (mf.Checked)
                    {
                        matching_file.addReference();
                    }
                }
            }
            else
            {
                not_found.Add(comb);
            }
        }
        private void CheckClick(object sender, EventArgs e)
        {
            if (this._Parent == null)
            {
                return;
            }

            if (!this._Parent.CheckedClicked(this, this.checkBox1.Checked))
            {
                this._Parent = null; // Abandon!
            }
        }
        public void setup(ManifestFile mf, int section)
        {
            this._Parent?.ForgetChild(this);

            this._Parent  = mf;
            this._Section = section;

            // Set all things that won't change while this has same parent
            this.lName.Text           = mf._StringPath;
            this.radioButton1.Enabled = this._Parent._BoolFileExists; // not given to parent

            // Update things that will change while this has same parent
            this._Parent._Child = this;
            this._Parent.UpdateLinkedGuiElements();
        }
Exemple #4
0
        void SaveContentToXML(TagItem ti, System.IO.StreamWriter sw, int indent)
        {
            bool         save = true;
            ManifestFile mf   = null;

            if (this._DictItemsByNode.TryGetValue(ti, out mf))
            {
                save = mf.Checked;
            }

            if (save)
            {
                if (ti is TagFile)
                {
                    var tf = ti as TagFile;
                    if (tf._Children.Length > 0)
                    {
                        String f, b;
                        tf.getStringXML(out f, out b);
                        if ((tf._Children.Length == 1) &&
                            ("text".Equals(tf._Name)) &&
                            (tf._Children[0] is TagText))
                        {
                            IndentWrite(sw, indent);
                            sw.Write(f);
                            sw.Write((tf._Children[0] as TagText)._Text);
                            sw.WriteLine(b);
                        }
                        else
                        {
                            IndentWrite(sw, indent, f);

                            if (tf == this._TagFileMetadata)
                            {
                                this.SaveMetaData(tf, sw, indent + 1);
                            }
                            else
                            {
                                switch (tf._Name)
                                {
                                case "navMap":
                                    Console.WriteLine("Single Find: \"navMap\"");
                                    this.SaveNavMap(tf, sw, indent);
                                    break;

                                case "docTitle":
                                    Console.WriteLine("Single Find: \"docTitle\"");
                                    this.SaveTitle(tf, sw, indent + 1);
                                    break;

                                case "guide":
                                    Console.WriteLine("Single Find: \"guide\"");
                                    this.SaveGuide(tf, sw, indent + 1);
                                    break;

                                default:
                                    foreach (var ch in tf._Children)
                                    {
                                        this.SaveContentToXML(ch, sw, indent + 1);
                                    }
                                    break;
                                }
                            }

                            IndentWrite(sw, indent, b);
                        }
                    }
                    else
                    {
                        String mid;
                        tf.getStringXML(out mid);
                        IndentWrite(sw, indent, mid);
                    }
                }
                else if (ti is TagText)
                {
                    var tt = ti as TagText;
                    IndentWrite(sw, indent, tt._Text);
                }
                else
                {
                    Console.WriteLine("Error: Saving not a file or text");
                }
            }
        }
Exemple #5
0
        private void bLook_Click(object sender, EventArgs e)
        {
            if (this._DoingStuff)
            {
                return;
            }
            try
            {
                this._DoingStuff         = true;
                this.bOpen.Enabled       = false;
                this.bLook.Enabled       = false;
                this.bClear.Enabled      = true;
                this.bGo.Enabled         = true;
                this.textBoxPath.Enabled = false;

                var book_path = this.textBoxPath.Text;

                var content_text  = new List <ManifestFileNavigation>();
                var content_other = new List <ManifestFile>();
                var content_image = new List <ManifestFile>();

                {
                    var tf = TagFile.ParseText(File.ReadAllText(Path.Combine(book_path, "content.opf")));
                    var package_matches = tf.getMatchesAtAnyDepth("package").ToList();
                    if (package_matches.Count != 1)
                    {
                        throw new Exception("Invalid XML 1");
                    }
                    if (!(package_matches[0] is TagFile))
                    {
                        throw new Exception("Invalid XML2");
                    }
                    this._TagFileBase = package_matches[0] as TagFile;

                    TagFile manifest = null;
                    TagFile spine    = null;
                    foreach (var node1 in this._TagFileBase._Children)
                    {
                        if (node1 is TagFile)
                        {
                            var node2 = node1 as TagFile;
                            switch (node2._Name)
                            {
                            case "manifest":
                                if (manifest == null)
                                {
                                    manifest = node2;
                                }
                                else
                                {
                                    Console.WriteLine("Error: Multiple Manifests");
                                }
                                break;

                            case "spine":
                                if (spine == null)
                                {
                                    spine = node2;
                                }
                                else
                                {
                                    Console.WriteLine("Error: Multiple Spines");
                                }
                                break;

                            case "metadata":
                                if (this._TagFileMetadata == null)
                                {
                                    this._TagFileMetadata = node2;
                                }
                                else
                                {
                                    Console.WriteLine("Error: Multiple Metadatas");
                                }
                                break;
                            }
                        }
                    }

                    foreach (var node3 in manifest._Children)
                    {
                        if (node3 is TagFile)
                        {
                            var node4   = node3 as TagFile;
                            var params_ = node4._Params;


                            var mt = new ManifestFile(node4, book_path);

                            bool include = true;

                            include &= !("toc.ncx".Equals(mt._StringPath));

                            if (include)
                            {
                                switch (mt._MediaType)
                                {
                                case ManifestFile.MediaType.Image:
                                    content_image.Add(mt);
                                    break;

                                case ManifestFile.MediaType.Text:
                                    mt = new ManifestFileNavigation(node4, book_path);
                                    content_text.Add(mt as ManifestFileNavigation);
                                    break;

                                case ManifestFile.MediaType.Other:
                                    content_other.Add(mt);
                                    break;
                                }

                                this._DictItemsByXMLID[mt._StringID]  = mt;
                                this._DictItemsByPath[mt._StringPath] = mt;
                                this._DictItemsByNode[node4]          = mt;
                                this._ListItems.Add(mt);
                            }
                        }
                    }

                    int spine_count = 0;
                    foreach (var node3 in spine._Children)
                    {
                        if (node3 is TagFile)
                        {
                            var node4   = node3 as TagFile;
                            var params_ = node4._Params;

                            ManifestFile relative = null;
                            String       idref    = params_["idref"];
                            if (this._DictItemsByXMLID.TryGetValue(idref, out relative))
                            {
                                this._DictItemsByNode[node4] = relative;
                                relative._IntSpineCount      = spine_count++;
                            }
                            else
                            {
                                Console.WriteLine("Error, no corresponding manifest item for spine item: " + idref);
                            }
                        }
                    }

                    bool found_author = false;
                    bool found_title  = false;
                    bool found_genre  = false;
                    foreach (var node3 in this._TagFileMetadata._Children)
                    {
                        if (node3 is TagFile)
                        {
                            var node4 = node3 as TagFile;
                            switch (node4._Name)
                            {
                            case "dc:creator":
                            {
                                if (found_author)
                                {
                                    Console.Write("Error: Second Author Found: ");
                                    node4.Display();
                                }
                                else if (node4._Children.Length == 1)
                                {
                                    if (node4._Children.First() is TagText)
                                    {
                                        found_author            = true;
                                        this.textBoxAuthor.Text = (node4._Children.First() as TagText)._Text;
                                    }
                                    else
                                    {
                                        Console.Write("Error: Author Wrong Type of Children: ");
                                        node4.Display();
                                    }
                                }
                                else
                                {
                                    Console.Write("Error: Author Wrong Number of Children: ");
                                    node4.Display();
                                }
                            }
                            break;

                            case "dc:title":
                            {
                                if (found_title)
                                {
                                    Console.Write("Error: Second Title Found: ");
                                    node4.Display();
                                }
                                else if (node4._Children.Length == 1)
                                {
                                    if (node4._Children.First() is TagText)
                                    {
                                        found_title            = true;
                                        this.textBoxTitle.Text = (node4._Children.First() as TagText)._Text;
                                    }
                                    else
                                    {
                                        Console.Write("Error: Title Wrong Type of Children: ");
                                        node4.Display();
                                    }
                                }
                                else
                                {
                                    Console.Write("Error: Title Wrong Number of Children: ");
                                    node4.Display();
                                }
                            }
                            break;

                            case "dc:genre":
                            {
                                if (found_genre)
                                {
                                    Console.Write("Error: Second Genre Found: ");
                                    node4.Display();
                                }
                                else if (node4._Children.Length == 1)
                                {
                                    if (node4._Children.First() is TagText)
                                    {
                                        found_genre            = true;
                                        this.textBoxGenre.Text = (node4._Children.First() as TagText)._Text;
                                    }
                                    else
                                    {
                                        Console.Write("Error: Genre Wrong Type of Children: ");
                                        node4.Display();
                                    }
                                }
                                else
                                {
                                    Console.Write("Error: Genre Wrong Number of Children: ");
                                    node4.Display();
                                }
                            }
                            break;
                            }
                        }
                    }

                    var missing_files = new HashSet <String>();

                    foreach (ManifestFile mf in content_text)
                    {
                        if (mf._BoolFileExists)
                        {
                            int    last_dex = mf._StringPath.LastIndexOf("/");
                            String dir      = last_dex > 0 ? mf._StringPath.Substring(0, last_dex + 1) : "";

                            TagFile tf2 = TagFile.ParseText(File.ReadAllText(mf._StringPathFull));

                            foreach (var ls in tf2.getMatchesAtAnyDepth("link"))
                            {
                                this.haveEmbeddedItem(ls._Params["href"], dir, mf, missing_files);
                            }
                            foreach (var ls in tf2.getMatchesAtAnyDepth("img"))
                            {
                                this.haveEmbeddedItem(ls._Params["src"], dir, mf, missing_files);
                            }
                            foreach (var ls in tf2.getMatchesAtAnyDepth("image"))
                            {
                                this.haveEmbeddedItem(ls._Params["xlink:href"], dir, mf, missing_files);
                            }

                            foreach (var ls in tf2.getMatchesAtAnyDepth("style"))
                            {
                                foreach (var ls2 in ls._Children)
                                {
                                    if (ls2 is TagText)
                                    {
                                        String text = (ls2 as TagText)._Text;

                                        int dex  = -1;
                                        int dlex = 1;

                                        int count = -1;
                                        while (dlex > 0)
                                        {
                                            count++;
                                            dex  = dlex;
                                            dlex = text.IndexOf("@font-face", dex + 1);

                                            if (dlex > 0)
                                            {
                                                int first = text.IndexOf("{", dlex);
                                                int last  = text.IndexOf("}", dlex);
                                                var path  = FontFaceSerializer.getFontPath(
                                                    text.Substring(first + 1, last - first - 1));
                                                if (path != null)
                                                {
                                                    this.haveEmbeddedItem(path, dir, mf, missing_files);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    foreach (var comb in missing_files)
                    {
                        Console.WriteLine("Error: Missing File Reference: " + comb);
                    }

                    // Text with references get moved to other
                    for (int i = 0; i < content_text.Count; i++)
                    {
                        ManifestFile mf = content_text[i];
                        if (mf._IntRefrencesMax != 0)
                        {
                            content_text.RemoveAt(i);
                            content_other.Add(mf);
                            i--;
                        }
                    }

                    content_text.Sort();
                }


                { // Get table of contents data
                    var tf       = TagFile.ParseText(File.ReadAllText(Path.Combine(book_path, "toc.ncx")));
                    var nav_maps = tf.getMatchesAtAnyDepth("ncx", "navMap").ToList();
                    if (nav_maps.Count != 1)
                    {
                        MessageBox.Show("Invalid XML 1: toc.ncx");
                        return;
                    }
                    if (!(nav_maps[0] is TagFile))
                    {
                        MessageBox.Show("Invalid XML 2: toc.ncx");
                        return;
                    }

                    var name_path_indent_s = new List <Tuple <string, string, int> >();
                    Action <TagItem, int> recursive_search = null;
                    recursive_search = (TagItem node, int level) =>
                    {
                        if (node is TagFile)
                        {
                            foreach (var child in (node as TagFile)._Children)
                            {
                                recursive_search(child, level + 1);
                            }
                        }
                        else if (node is TagText)
                        {
                            if (node.MatchesHeirarchicalNaming(null, "text", "navLabel", "navPoint"))
                            {
                                var content_array = node.GetParent(3)?.getMatchesAtZeroDepth("content")?.ToArray();

                                if (content_array == null)
                                {
                                    return;
                                }
                                if (content_array.Length != 1)
                                {
                                    return;
                                }
                                if (!(content_array[0] is TagFile))
                                {
                                    return;
                                }

                                var content = content_array[0] as TagFile;

                                String path;
                                if (content._Params.TryGetValue("src", out path))
                                {
                                    // - 3 for the 3 parents to get to root!
                                    int    tab_index = level - 3;
                                    String chap_name = (node as TagText)._Text;
                                    name_path_indent_s.Add(new Tuple <string, string, int>(chap_name, path, tab_index));
                                    for (int i = 0; i < tab_index; i++)
                                    {
                                        Console.Write(" ");
                                    }
                                    Console.WriteLine(chap_name + " " + path);
                                }
                            }
                        }
                    };

                    recursive_search(nav_maps[0] as TagFile, 0);

                    if (name_path_indent_s.Count > 0)
                    {
                        var key_path_value_name_closes = new Dictionary <string, Tuple <string, int> >();

                        int min_index = name_path_indent_s.Select(s => s.Item3).Min();

                        for (int i = 0; i < name_path_indent_s.Count; i++)
                        {
                            var    tup       = name_path_indent_s[i];
                            String name      = tup.Item1;
                            String path      = tup.Item2;
                            int    tab_index = tup.Item3 - min_index;

                            int next_tab_index = (i == name_path_indent_s.Count - 1)
                                ? 0 : (name_path_indent_s[i + 1].Item3 - min_index);

                            key_path_value_name_closes[path] = new Tuple <string, int>(
                                name, 1 + tab_index - next_tab_index);
                        }

                        foreach (var mfn in content_text)
                        {
                            Tuple <string, int> tup;
                            if (key_path_value_name_closes.TryGetValue(mfn._StringPath, out tup))
                            {
                                mfn._NavigationName        = tup.Item1;
                                mfn._NavigationType        = ManifestFileNavigation.NavigationType.Custom;
                                mfn._NavigationPointCloses = tup.Item2;

                                mfn.UpdateAutoInclude(tup.Item1); // "Chapter" "Part" "Epilogue"
                            }
                        }
                    }
                }



                this._ItemImageHandler._Content = content_image.ToArray();
                this._ItemTextHandler.SetContent(content_text);
                this._ItemOtherHandler._Content = content_other.ToArray();

                this.uiTableView1.ReloadData();
                this.uiTableView2.ReloadData();
                this.uiTableView3.ReloadData();
            }
            finally
            {
                this._DoingStuff = false;
            }
        }
Exemple #6
0
        public void saveNavPoint(TagFile np, ref int play_order, StreamWriter sw, int indent)
        {
            if (np._Params.ContainsKey("playOrder"))
            {
                np._Params["playOrder"] = play_order.ToString();
            }

            bool save = false;

            foreach (var ti_c in np._Children)
            {
                if (ti_c is TagFile)
                {
                    var tf_c = ti_c as TagFile;
                    if ("content".Equals(tf_c._Name))
                    {
                        String src = null;
                        if (tf_c._Params.TryGetValue("src", out src))
                        {
                            ManifestFile mf = null;
                            if (this._DictItemsByPath.TryGetValue(src, out mf))
                            {
                                if (mf.Checked)
                                {
                                    save = true;
                                    play_order++;
                                    break;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Error: navPoint missing ManifestFile: " + src);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error: navPoint no source on content");
                        }
                    }
                }
            }


            if (save)
            {
                String f, b;
                np.getStringXML(out f, out b);
                for (int i = 0; i < indent; i++)
                {
                    sw.Write('\t');
                }
                sw.WriteLine(f);

                foreach (var ti_c in np._Children)
                {
                    if (ti_c is TagFile)
                    {
                        var tf_c = ti_c as TagFile;
                        if ("navPoint".Equals(tf_c._Name))
                        {
                            this.saveNavPoint(tf_c, ref play_order, sw, indent + 1);
                        }
                        else
                        {
                            this.SaveContentToXML(ti_c, sw, indent + 1);
                        }
                    }
                    else
                    {
                        this.SaveContentToXML(ti_c, sw, indent + 1);
                    }
                }
                for (int i = 0; i < indent; i++)
                {
                    sw.Write('\t');
                }
                sw.WriteLine(b);
            }
        }