public static bool HasField(BibTexItem bibtex_item, string field)
 {
     if (null == bibtex_item)
     {
         return(false);
     }
     return(bibtex_item.ContainsField(field));
 }
        private void EnsureStartPageOffset()
        {
            try
            {
                // -1 means we are not initialised
                if (-1 == start_page_offset)
                {
                    // 0 means that there is no known offset
                    start_page_offset = 0;

                    BibTexItem bibtex_item = pdf_document.BibTexItem;
                    if (null != bibtex_item)
                    {
                        string start_page_offset_text = null;
                        if (String.IsNullOrEmpty(start_page_offset_text) && bibtex_item.ContainsField("page"))
                        {
                            start_page_offset_text = bibtex_item["page"];
                        }
                        if (String.IsNullOrEmpty(start_page_offset_text) && bibtex_item.ContainsField("pages"))
                        {
                            start_page_offset_text = bibtex_item["pages"];
                        }

                        if (!String.IsNullOrEmpty(start_page_offset_text))
                        {
                            MatchCollection matches = Regex.Matches(start_page_offset_text, @"(\d+).*");
                            if (0 < matches.Count && 1 < matches[0].Groups.Count)
                            {
                                string start_page_offset_string = matches[0].Groups[1].Value;
                                start_page_offset = Convert.ToInt32(start_page_offset_string);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ex, "Problem calculating start_page_offset");
            }
        }
        public static void SetGenericPublication(BibTexItem bibtex_item, string generic_publication)
        {
            if (null == bibtex_item)
            {
                return;
            }

            bool set_a_field = false;

            if (bibtex_item.ContainsField("journal"))
            {
                set_a_field            = true;
                bibtex_item["journal"] = generic_publication;
            }

            if (bibtex_item.ContainsField("booktitle"))
            {
                set_a_field = true;
                bibtex_item["booktitle"] = generic_publication;
            }

            if (bibtex_item.ContainsField("container-title"))
            {
                set_a_field = true;
                bibtex_item["container-title"] = generic_publication;
            }

            if (bibtex_item.ContainsField("publisher"))
            {
                set_a_field = true;
                bibtex_item["publisher"] = generic_publication;
            }

            // If no field was ever set, insert a new field.
            // NB: This could get smarter in that we don't always want to insert journal, depending on bibtex type
            if (!set_a_field)
            {
                bibtex_item["journal"] = generic_publication;
            }
        }
Пример #4
0
        internal static void Export(WebLibraryDetail web_library_detail, List <PDFDocument> pdf_documents, string base_path, Dictionary <string, PDFDocumentExportItem> pdf_document_export_items)
        {
            Logging.Info("Exporting entries to BibTeXTAB separated");

            // First work out what fields are available
            List <string> field_names = null;
            {
                HashSet <string> field_names_set = new HashSet <string>();
                for (int i = 0; i < pdf_documents.Count; ++i)
                {
                    PDFDocument pdf_document = pdf_documents[i];
                    if (!String.IsNullOrEmpty(pdf_document.BibTex))
                    {
                        BibTexItem item = BibTexParser.ParseOne(pdf_document.BibTex, true);
                        if (null != item)
                        {
                            foreach (var field in item.Fields)
                            {
                                field_names_set.Add(field.Key.ToLower());
                            }
                        }
                    }
                }

                field_names = new List <string>(field_names_set);
                field_names.Sort();
            }

            // Write out the header
            DateTime      now = DateTime.Now;
            StringBuilder sb  = new StringBuilder();

            sb.AppendLine("% -------------------------------------------------------------------------");
            sb.AppendLine(String.Format("% This tab separated file was generated by Qiqqa ({0}?ref=EXPTAB)", Common.Configuration.WebsiteAccess.Url_Documentation4Qiqqa));
            sb.AppendLine(String.Format("% {0} {1}", now.ToLongDateString(), now.ToLongTimeString()));
            sb.AppendLine("% Version 1");
            sb.AppendLine("% -------------------------------------------------------------------------");
            sb.AppendLine();

            // Headers
            sb.AppendFormat("{0}\t", "Fingerprint");
            sb.AppendFormat("{0}\t", "Filename");
            sb.AppendFormat("{0}\t", "BibTexKey");
            sb.AppendFormat("{0}\t", "BibTexType");
            foreach (string field_name in field_names)
            {
                sb.AppendFormat("{0}\t", FormatFreeText(field_name));
            }
            sb.AppendLine();

            // Write out the entries
            for (int i = 0; i < pdf_documents.Count; ++i)
            {
                StatusManager.Instance.UpdateStatus("TabExport", String.Format("Exporting entry {0} of {1}", i, pdf_documents.Count), i, pdf_documents.Count);

                PDFDocument pdf_document = pdf_documents[i];
                sb.AppendFormat("{0}\t", pdf_document.Fingerprint);
                sb.AppendFormat("{0}\t", pdf_document_export_items.ContainsKey(pdf_document.Fingerprint) ? pdf_document_export_items[pdf_document.Fingerprint].filename : "");

                try
                {
                    if (!String.IsNullOrEmpty(pdf_document.BibTex))
                    {
                        BibTexItem item = BibTexParser.ParseOne(pdf_document.BibTex, true);
                        if (null != item)
                        {
                            sb.AppendFormat("{0}\t", item.Key);
                            sb.AppendFormat("{0}\t", item.Type);
                            foreach (string field_name in field_names)
                            {
                                sb.AppendFormat("{0}\t", item.ContainsField(field_name) ? FormatFreeText(item[field_name]) : "");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.Error(ex, "There was a problem exporting the tab representation for document {0}", pdf_document.Fingerprint);
                }

                sb.AppendLine();
            }

            // Write to disk
            string filename = Path.GetFullPath(Path.Combine(base_path, @"Qiqqa.BibTeX.tab"));

            File.WriteAllText(filename, sb.ToString());

            StatusManager.Instance.UpdateStatus("TabExport", String.Format("Exported your BibTeX tab entries to {0}", filename));
        }