Docear4Word.DocumentController.CreateInlineCitation C# (CSharp) Method

CreateInlineCitation() private method

private CreateInlineCitation ( IEnumerable itemSources, object idToUse = null ) : Docear4Word.JSInlineCitation
itemSources IEnumerable
idToUse object
return Docear4Word.JSInlineCitation
        JSInlineCitation CreateInlineCitation(IEnumerable<EntryAndPagePair> itemSources, object idToUse = null)
        {
            // ****IMPORTANT****
            // This is called from InsertCitationSequence, InsertCitation, EditCitation and CitationInserter.UpdateCitationsFromDatabase
            //
            // It is imperative that calls from the first 3 work on an empty CiteProc otherwise the cache gets used to create
            // the citation items. Other than first-use, this means using the item after CiteProc has seen it and maybe modified it
            // (it appears to change the Date Parts to strings in some cases)
            // The next refresh is then comparing incorrect JSON and will want to update it from the database.
            //
            // (CitationInserter.UpdateCitationsFromDatabase calls here but this is always within a Refresh which means a brand new CiteProc anyway
            // and so multiple resets here are not a problem because the raw cache would be empty anyway)
            CiteProc.ResetProcessorState();

            var result = new JSInlineCitation(CiteProc);

            if (idToUse != null)
            {
                result.CitationID = idToUse;
            }

            result.Properties.NoteIndex = 0;

            foreach(var itemSource in itemSources)
            {
                var inlineCitationItem = CiteProc.CreateJSInlineCitationItem(itemSource);

                result.CitationItems.Add(inlineCitationItem);
            }

            // We store this before Citeproc gets hold of it!
            result.FieldCodeJSON = CiteProc.ToJSON(result.JSObject, JSONWhitespace).Replace('\n', '\v') + FieldCodeSeparator;

            return result;
        }

Usage Example

Example #1
0
            int UpdateCitationsFromDatabase(BibTexDatabase database)
            {
                if (database == null || !Settings.Instance.RefreshUpdatesCitationsFromDatabase)
                {
                    return(-1);
                }

                var changesMade = 0;

                foreach (var cslField in documentController.EnumerateCSLFields())
                {
                    if (!IsCitationField(cslField))
                    {
                        continue;
                    }

                    var existingCitation = GetCitation(cslField.Code.Text);

                    var sources = Helper.ExtractSources(existingCitation, database);

                    // We can't risk losing a whole citation cluster because one item is not in the database
                    // so we skip the compare and keep the inline version
                    if (sources.Count != existingCitation.CitationItems.Length)
                    {
                        continue;
                    }

                    // We also won't update a cluster containing unknown items
                    // since this behaviour is undefined
                    if (ContainsUnknownItems(sources))
                    {
                        continue;
                    }

                    var databaseCitation = documentController.CreateInlineCitation(sources, existingCitation.CitationID);
                    Debug.Assert(existingCitation.JSObject != databaseCitation.JSObject);

                    if (DatabaseVersionIsDifferent(existingCitation, databaseCitation))
                    {
                        // Change the Field Code Text
                        cslField.Code.Text = CreateFieldCodeText(databaseCitation);
                        FormatCitationField(cslField);

                        // Increment the changed counter
                        changesMade++;
                    }
                }

                return(changesMade);
            }