Esempio n. 1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the next paratext segment for import.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void GetNextParatextSegment(out string sText, out string sMarker)
        {
            sMarker = null;
            sText   = string.Empty;
            while (m_ptCurrentToken < m_ptBookTokens.Count)
            {
                UsfmToken token = m_ptBookTokens[m_ptCurrentToken];
                if (token.Type == UsfmTokenType.Text)
                {
                    sText += token.Text;
                }
                else
                {
                    if (sMarker != null)
                    {
                        break;                         // Found another marker so we got all the text for this one
                    }
                    sMarker = @"\" + token.Marker;     // We expect the marker to have the slash
                }
                m_ptParserState.UpdateState(m_ptBookTokens, m_ptCurrentToken);

                if (!ScrImportFileInfo.IsValidMarker(sMarker))
                {
                    throw new ScriptureUtilsException(SUE_ErrorCode.InvalidCharacterInMarker, null, 0,
                                                      sMarker + sText, SegmentFirstRef);
                }
                m_ptCurrentToken++;
            }
        }
Esempio n. 2
0
 public PT7TokenWrapper(UsfmToken token)
 {
     pt7Token = token;
 }
Esempio n. 3
0
 public PT8TokenWrapper(UsfmToken token)
 {
     ptToken = token;
 }
Esempio n. 4
0
        /// <summary> Write up-to-date book text from mongo database to Paratext project folder. </summary>
        public async Task PutBookText(UserSecret userSecret, string projectId, int bookNum, string usx,
                                      Dictionary <int, string> chapterAuthors = null)
        {
            string  username = GetParatextUsername(userSecret);
            ScrText scrText  = ScrTextCollection.FindById(username, projectId);
            var     doc      = new XmlDocument
            {
                PreserveWhitespace = true
            };

            doc.LoadXml(usx);
            UsxFragmenter.FindFragments(scrText.ScrStylesheet(bookNum), doc.CreateNavigator(),
                                        XPathExpression.Compile("*[false()]"), out string usfm);
            usfm = UsfmToken.NormalizeUsfm(scrText.ScrStylesheet(bookNum), usfm, false, scrText.RightToLeft, scrText);

            if (chapterAuthors == null || chapterAuthors.Count == 0)
            {
                // If we don't have chapter authors, update book as current user
                if (scrText.Permissions.AmAdministrator)
                {
                    // if the current user is an administrator, then always allow editing the book text even if the user
                    // doesn't have permission. This will ensure that a sync by an administrator never fails.
                    scrText.Permissions.RunWithEditPermision(bookNum,
                                                             () => scrText.PutText(bookNum, 0, false, usfm, null));
                }
                else
                {
                    scrText.PutText(bookNum, 0, false, usfm, null);
                }
                _logger.LogInformation("{0} updated {1} in {2}.", userSecret.Id,
                                       Canon.BookNumberToEnglishName(bookNum), scrText.Name);
            }
            else
            {
                // As we have a list of chapter authors, build a dictionary of ScrTexts for each of them
                Dictionary <string, ScrText> scrTexts = new Dictionary <string, ScrText>();
                foreach (string userId in chapterAuthors.Values.Distinct())
                {
                    if (userId == userSecret.Id)
                    {
                        scrTexts.Add(userId, scrText);
                    }
                    else
                    {
                        // Get their user secret, so we can get their username, and create their ScrText
                        UserSecret authorUserSecret = await _userSecretRepository.GetAsync(userId);

                        string authorUserName = GetParatextUsername(authorUserSecret);
                        scrTexts.Add(userId, ScrTextCollection.FindById(authorUserName, projectId));
                    }
                }

                // If there is only one author, just write the book
                if (scrTexts.Count == 1)
                {
                    scrTexts.Values.First().PutText(bookNum, 0, false, usfm, null);
                    _logger.LogInformation("{0} updated {1} in {2}.", scrTexts.Keys.First(),
                                           Canon.BookNumberToEnglishName(bookNum), scrText.Name);
                }
                else
                {
                    // Split the usfm into chapters
                    List <string> chapters = ScrText.SplitIntoChapters(scrText.Name, bookNum, usfm);

                    // Put the individual chapters
                    foreach ((int chapterNum, string authorUserId) in chapterAuthors)
                    {
                        if ((chapterNum - 1) < chapters.Count)
                        {
                            // The ScrText permissions will be the same as the last sync's permissions, so no need to check
                            scrTexts[authorUserId].PutText(bookNum, chapterNum, false, chapters[chapterNum - 1], null);
                            _logger.LogInformation("{0} updated chapter {1} of {2} in {3}.", authorUserId,
                                                   chapterNum, Canon.BookNumberToEnglishName(bookNum), scrText.Name);
                        }
                    }
                }
            }
        }