Esempio n. 1
0
    def get_body_html(self):
        obs_sq_html = f'''
<section id="{self.lang_code}-obs-sq">
    <div class="resource-title-page">
        <img src="{self.resources['obs-sq'].logo_url}" class="logo" alt="OBS">
        <h1 class="section-header">{self.simple_title}</h1>
    </div>
'''
        files = sorted(glob(os.path.join(self.main_resource.repo_dir, 'content', '*.md')))
        for file in files:
            chapter_num = os.path.splitext(os.path.basename(file))[0]
            chapter_html = markdown2.markdown_path(file)
            chapter_html = html_tools.increment_headers(chapter_html)
            soup = BeautifulSoup(chapter_html, 'html.parser')
            headers = soup.find_all(re.compile(r'^h\d'))
            top_header = headers[0]
            title = top_header.text
            header_count = 1
            for header in headers:
                header['class'] = 'section-header'
                header['id'] = f'{self.lang_code}-obs-sq-{chapter_num}-{header_count}'
                header_count += 1
            # HANDLE OBS SQ RC CHAPTER LINKS
            obs_sq_rc_link = f'rc://{self.lang_code}/obs-sq/help/{chapter_num}'
            obs_sq_rc = self.add_rc(obs_sq_rc_link, title=title, article=chapter_html)
            chapter_data = obs_tools.get_obs_chapter_data(self.resources['obs'].repo_dir, chapter_num)
            if len(chapter_data['frames']):
                frames_html = '<div class="obs-frames">\n'
                for idx, frame in enumerate(chapter_data['frames']):
                    frame_num = str(idx+1).zfill(2)
                    frame_title = f'{chapter_num}:{frame_num}'
                    # HANDLE FRAME RC LINKS FOR OBS
                    frame_rc_link = f'rc://{self.lang_code}/obs/book/obs/{chapter_num}/{frame_num}'
                    frame_rc = self.add_rc(frame_rc_link, title=frame_title)
                    frames_html += f'''
    <div id={frame_rc.article_id} class="obs-frame">
        <div class="obs-frame-title">
            {frame_title}
        </div>
        <div class="obs-frame-text">
            {frame['text']}
        </div>
    </div>
'''
                frames_html += '</div>\n'
                top_header.insert_after(BeautifulSoup(frames_html, 'html.parser'))
                bible_reference_html = f'''
    <div class="bible-reference">
        {chapter_data['bible_reference']}
    </div>
'''
                top_header.insert_after(BeautifulSoup(bible_reference_html, 'html.parser'))

            article_html = f'''
    <article id="{obs_sq_rc.article_id}">
        {str(soup)}
    </article>
'''
            obs_sq_html += article_html
        return obs_sq_html
    def get_tq_html(self):
        tq_html = ''
        if self.project_id:
            projects = [self.main_resource.find_project(self.project_id)]
        else:
            projects = self.main_resource.projects
        # for project_idx, project in enumerate(projects):
        #   project_id = project['identifier']
# REMOVE FROM HERE TO "REMOVE TO HERE" and uncomment above to use the manifest projects once they are sorted
        if self.project_id:
            project_ids = [self.project_id]
        else:
            project_ids = BOOK_NUMBERS.keys()
        for project_idx, project_id in enumerate(project_ids):
            project = self.main_resource.find_project(project_id)
# REMOVE TO HERE
            book_title = self.get_project_title(project)
            project_dir = os.path.join(self.main_resource.repo_dir, project_id)
            chapter_dirs = sorted(glob(os.path.join(project_dir, '*')))
            tq_html += f'''
<section id="{self.lang_code}-{self.name}-{project_id}" class="tq-book">
    <article id="{self.lang_code}-{self.name}-{project_id}-cover" class="resource-title-page no-header-footer"">
        <img src="{self.main_resource.logo_url}" class="logo" alt="UTQ">
        <h1{' class="section-header"' if project_idx == 0 else ''}>{self.title}</h1>
        <h2 class="section-header no-header">{book_title}</h2>
    </article>
'''
            for chapter_dir in chapter_dirs:
                chapter = os.path.basename(chapter_dir).lstrip('0')
                tq_html += f'''
    <section id="{self.lang_code}-{self.name}-{project_id}-{self.pad(chapter)}" class="tq-chapter">
        <h3 class="section-header{' no-toc' if len(projects) > 1 else ''}">{book_title} {chapter}</h3>
'''
                verse_files = sorted(glob(os.path.join(chapter_dir, '*.md')))
                for verse_file in verse_files:
                    verse = os.path.splitext(os.path.basename(verse_file))[0].lstrip('0')
                    tq_article = markdown2.markdown_path(verse_file)
                    tq_article = increment_headers(tq_article, 3)
                    tq_title = f'{book_title} {chapter}:{verse}'
                    tq_rc_link = f'rc://{self.lang_code}/tq/help/{project_id}/{self.pad(chapter, project_id)}/{verse.zfill(3)}'
                    tq_rc = self.add_rc(tq_rc_link, tq_article, tq_title)
                    tq_html += f'''
        <article id="{tq_rc.article_id}" class="tq-verse">
            <h3>{tq_rc.title}</h3>
            {tq_article}
        </article>
'''
                tq_html += '''
    </section>
'''
            tq_html += '''
</section>
'''
        self.logger.info('Done generating TQ HTML.')
        return tq_html
Esempio n. 3
0
    def get_body_html(self):
        self.logger.info('Generating OBS SN SQ html...')
        obs_sn_sq_html = f'''
<section id="{self.lang_code}-obs-sn">
    <div class="resource-title-page no-header">
        <img src="{self.resources['obs-sn'].logo_url}" class="logo" alt="OBS">
        <h1 class="section-header">{self.simple_title}</h1>
    </div>
'''
        intro_file = os.path.join(self.resources['obs-sq'].repo_dir, 'content',
                                  '00.md')
        if os.path.isfile(intro_file):
            intro_id = 'obs-sq-intro'
            intro_content = markdown2.markdown_path(intro_file)
            intro_content = html_tools.increment_headers(intro_content, 1)
            intro_content = intro_content.replace(
                '<h2>', '<h2 class="section-header">', 1)
            obs_sn_sq_html += f'''
    <article id="{intro_id}">
        {intro_content}
    </article>
'''
        for chapter_num in range(1, 51):
            chapter_num = str(chapter_num).zfill(2)
            sn_chapter_dir = os.path.join(self.resources['obs-sn'].repo_dir,
                                          'content', chapter_num)
            sq_chapter_file = os.path.join(self.resources['obs-sq'].repo_dir,
                                           'content', f'{chapter_num}.md')
            obs_chapter_data = obs_tools.get_obs_chapter_data(
                self.resources['obs'].repo_dir, chapter_num)
            chapter_title = obs_chapter_data['title']
            # HANDLE RC LINKS FOR OBS SN CHAPTER
            obs_sn_chapter_rc_link = f'rc://{self.lang_code}/obs-sn/help/obs/{chapter_num}'
            obs_sn_chapter_rc = self.add_rc(obs_sn_chapter_rc_link,
                                            title=chapter_title)
            obs_sn_sq_html += f'''
    <section id="{obs_sn_chapter_rc.article_id}">
        <h2 class="section-header">{chapter_title}</h2>
        <section id="{obs_sn_chapter_rc.article_id}-notes" class="no-break">
            <h3 class="section-header no-break">{self.translate('study_notes')}</h3>
'''
            if 'bible_reference' in obs_chapter_data and obs_chapter_data[
                    'bible_reference']:
                obs_sn_sq_html += f'''
                    <div class="bible-reference" class="no-break">{obs_chapter_data['bible_reference']}</div>
            '''
            frames = obs_chapter_data['frames']
            for frame_idx, frame in enumerate(frames):
                image = frame['image']
                frame_num = str(frame_idx + 1).zfill(2)
                frame_title = f'{chapter_num}:{frame_num}'
                obs_sn_file = os.path.join(sn_chapter_dir, f'{frame_num}.md')

                if os.path.isfile(obs_sn_file):
                    notes_html = markdown2.markdown_path(obs_sn_file)
                    notes_html = html_tools.increment_headers(notes_html, 3)
                else:
                    no_study_notes = self.translate(
                        'no_study_notes_for_this_frame')
                    notes_html = f'<div class="no-notes-message">({no_study_notes})</div>'

                # HANDLE RC LINKS FOR OBS SN FRAME
                obs_sn_rc_link = f'rc://{self.lang_code}/obs-sn/help/obs/{chapter_num}/{frame_num}'
                obs_sn_rc = self.add_rc(obs_sn_rc_link,
                                        title=frame_title,
                                        article=notes_html)
                # HANDLE RC LINKS FOR OBS FRAME
                obs_rc_link = f'rc://{self.lang_code}/obs/book/obs/{chapter_num}/{frame_num}'
                self.add_rc(obs_rc_link,
                            title=frame_title,
                            article_id=obs_sn_rc.article_id)

                obs_text = ''
                if frame['text'] and notes_html:
                    obs_text = frame['text']
                    orig_obs_text = obs_text
                    phrases = html_tools.get_phrases_to_highlight(
                        notes_html, 'h4')
                    if phrases:
                        for phrase in phrases:
                            alignment = alignment_tools.split_string_into_alignment(
                                phrase)
                            marked_obs_text = html_tools.mark_phrases_in_html(
                                obs_text, alignment)
                            if not marked_obs_text:
                                self.add_bad_highlight(obs_sn_rc,
                                                       orig_obs_text,
                                                       obs_sn_rc.rc_link,
                                                       phrase)
                            else:
                                obs_text = marked_obs_text

                obs_sn_sq_html += f'''
        <article id="{obs_sn_rc.article_id}">
          <h4>{frame_title}</h4>
          <div class="obs-img-and-text">
            <img src="{image}" class="obs-img"/>
            <div class="obs-text">
                {obs_text}
            </div>
          </div>
          <div class="obs-sn-notes">
            {notes_html}
          </div>
        </article>
'''
            obs_sn_sq_html += '''
    </section>
'''
            if os.path.isfile(sq_chapter_file):
                obs_sq_title = f'{self.translate("study_questions")}'
                obs_sq_html = markdown2.markdown_path(sq_chapter_file)
                obs_sq_html = html_tools.increment_headers(obs_sq_html, 2)
                soup = BeautifulSoup(obs_sq_html, 'html.parser')
                header = soup.find(re.compile(r'^h\d'))
                header.decompose()
                obs_sq_html = str(soup)
                # HANDLE RC LINKS FOR OBS SQ
                obs_sq_rc_link = f'rc://{self.lang_code}/obs-sq/help/obs/{chapter_num}'
                obs_sq_rc = self.add_rc(obs_sq_rc_link,
                                        title=obs_sq_title,
                                        article=obs_sq_html)
                obs_sn_sq_html += f'''
        <article id="{obs_sq_rc.article_id}">
          <h3 class="section-header">{obs_sq_title}</h3>
          {obs_sq_html}
        </article>
    </section>
'''
        obs_sn_sq_html += '''
</section>
'''
        return obs_sn_sq_html
    def get_body_html(self):
        self.logger.info('Generating OBS SN html...')
        obs_sn_html = f'''
<section id="{self.lang_code}-obs-sn">
    <div class="resource-title-page no-header">
        <img src="{self.resources['obs-sn'].logo_url}" class="logo" alt="OBS">
        <h1 class="section-header">{self.simple_title}</h1>
    </div>
'''
        for chapter in range(1, 51):
            chapter_num = str(chapter).zfill(2)
            sn_chapter_dir = os.path.join(self.resources['obs-sn'].repo_dir, 'content', chapter_num)
            chapter_data = obs_tools.get_obs_chapter_data(self.resources['obs'].repo_dir, chapter_num)
            obs_sn_html += f'<article id="{self.lang_code}-obs-sn-{chapter_num}">\n\n'
            obs_sn_html += f'<h2 class="section-header">{chapter_data["title"]}</h2>\n'
            if 'bible_reference' in chapter_data and chapter_data['bible_reference']:
                obs_sn_html += f'''
                    <div class="bible-reference no-break">{chapter_data['bible_reference']}</div>
'''
            for frame_idx, frame in enumerate(chapter_data['frames']):
                frame_num = str(frame_idx+1).zfill(2)
                frame_title = f'{chapter_num}:{frame_num}'
                obs_sn_file = os.path.join(sn_chapter_dir, f'{frame_num}.md')

                if os.path.isfile(obs_sn_file):
                    notes_html = markdown2.markdown_path(obs_sn_file)
                    notes_html = html_tools.increment_headers(notes_html, 3)
                else:
                    no_study_notes = self.translate('no_study_notes_for_this_frame')
                    notes_html = f'<div class="no-notes-message">({no_study_notes})</div>'

                # HANDLE RC LINKS FOR OBS SN FRAMES
                obs_sn_rc_link = f'rc://{self.lang_code}/obs-sn/help/obs/{chapter_num}/{frame_num}'
                obs_sn_rc = self.add_rc(obs_sn_rc_link, title=frame_title, article=notes_html)
                # HANDLE RC LINKS FOR OBS FRAMES
                obs_rc_link = f'rc://{self.lang_code}/obs/bible/obs/{chapter_num}/{frame_num}'
                self.add_rc(obs_rc_link, title=frame_title, article_id=obs_sn_rc.article_id)

                obs_text = ''
                if frame['text'] and notes_html:
                    obs_text = frame['text']
                    orig_obs_text = obs_text
                    phrases = html_tools.get_phrases_to_highlight(notes_html, 'h4')
                    if phrases:
                        for phrase in phrases:
                            alignment = alignment_tools.split_string_into_alignment(phrase)
                            marked_obs_text = html_tools.mark_phrases_in_html(obs_text, alignment)
                            if not marked_obs_text:
                                self.add_bad_highlight(obs_sn_rc, orig_obs_text, obs_sn_rc.rc_link, phrase)
                            else:
                                obs_text = marked_obs_text

                obs_sn_html += f'''
<div id="{obs_sn_rc.article_id}" class="frame">
    <h3>{frame_title}</h3>
    <div id="{obs_sn_rc.article_id}-text" class="frame-text">
        {obs_text}
    </div>
    <div id="{obs_sn_rc.article_id}-notes" class="frame-notes">
        {notes_html}
    </div>
</div>
'''
                if frame_idx < len(chapter_data['frames']) - 1:
                    obs_sn_html += '<hr class="frame-divider"/>'
            obs_sn_html += '</article>\n\n'
        obs_sn_html += '</section>'
        return obs_sn_html
    def get_obs_tn_html(self):
        obs_tn_html = f'''
<section id="obs-sn">
    <div class="resource-title-page no-header">
        <img src="{self.resources['obs'].logo_url}" class="logo" alt="OBS">
        <h1 class="section-header">{self.simple_title}</h1>
    </div>
'''
        obs_tn_chapter_dirs = sorted(
            glob(os.path.join(self.main_resource.repo_dir, 'content', '*')))
        for obs_tn_chapter_dir in obs_tn_chapter_dirs:
            if os.path.isdir(obs_tn_chapter_dir):
                chapter_num = os.path.basename(obs_tn_chapter_dir)
                chapter_data = obs_tools.get_obs_chapter_data(
                    self.resources['obs'].repo_dir, chapter_num)
                obs_tn_html += f'''
    <article id="{self.lang_code}-obs-tn-{chapter_num}">
        <h2 class="section-header">{chapter_data['title']}</h2>
'''
                frames = [None] + chapter_data[
                    'frames']  # first item of '' if there are intro notes from the 00.md file
                for frame_idx, frame in enumerate(frames):
                    frame_num = str(frame_idx).zfill(2)
                    frame_title = f'{chapter_num}:{frame_num}'
                    notes_file = os.path.join(obs_tn_chapter_dir,
                                              f'{frame_num}.md')
                    notes_html = ''
                    if os.path.isfile(notes_file):
                        notes_html = markdown2.markdown_path(notes_file)
                        notes_html = html_tools.increment_headers(
                            notes_html, 3)
                    if (not frame or not frame['text']) and not notes_html:
                        continue

                    # HANDLE RC LINKS FOR OBS FRAME
                    frame_rc_link = f'rc://{self.lang_code}/obs/book/obs/{chapter_num}/{frame_num}'
                    frame_rc = self.add_rc(frame_rc_link, title=frame_title)
                    # HANDLE RC LINKS FOR NOTES
                    notes_rc_link = f'rc://{self.lang_code}/obs-tn/help/{chapter_num}/{frame_num}'
                    notes_rc = self.add_rc(notes_rc_link,
                                           title=frame_title,
                                           article=notes_html)

                    obs_text = ''
                    if frame and frame['text']:
                        obs_text = frame['text']
                        orig_obs_text = obs_text
                        if notes_html:
                            phrases = html_tools.get_phrases_to_highlight(
                                notes_html, 'h4')
                            for phrase in phrases:
                                alignment = alignment_tools.split_string_into_alignment(
                                    phrase)
                                marked_obs_text = html_tools.mark_phrases_in_html(
                                    obs_text, alignment)
                                if not marked_obs_text:
                                    if self.lang_code in TN_TITLES_TO_IGNORE and \
                                            phrase.lower() not in TN_TITLES_TO_IGNORE[self.lang_code]:
                                        self.add_bad_highlight(
                                            notes_rc, orig_obs_text,
                                            notes_rc.rc_link, phrase)
                                else:
                                    obs_text = marked_obs_text

                    if frame_idx == len(frames) - 1:
                        if 'bible_reference' in chapter_data and chapter_data[
                                'bible_reference']:
                            notes_html += f'''
                                <div class="bible-reference" class="no-break">{chapter_data['bible_reference']}</div>
                        '''
                    # Some OBS TN languages (e.g. English) do not have Translation Words in their TN article
                    # while some do (e.g. French). We need to add them ourselves from the tw_cat file
                    if notes_html and '/tw/' not in notes_html and chapter_num in self.tw_cat and \
                            frame_num in self.tw_cat[chapter_num] and len(self.tw_cat[chapter_num][frame_num]):
                        notes_html += f'''
           <h3>{self.resources['tw'].simple_title}</h3>
           <ul>
'''
                        for rc_link in self.tw_cat[chapter_num][frame_num]:
                            notes_html += f'''
                <li>[[{rc_link}]]</li>
'''
                        notes_html += '''
            </ul>
'''
                    notes_rc.set_article(notes_html)

                    if obs_text:
                        obs_text = f'''
            <div id="{frame_rc.article_id}" class="frame-text">
                {obs_text}
            </div>
'''
                    if notes_html:
                        notes_html = f'''
            <div id="{notes_rc.article_id}-notes" class="frame-notes">
                {notes_html}
            </div>
'''

                    obs_tn_html += f'''
        <div id="{notes_rc.article_id}">
            <h3>{frame_title}</h3>
            {obs_text}
            {notes_html}
        </div>
'''
                    if frame_idx < len(frames) - 1:
                        obs_tn_html += '<hr class="frame-divider"/>\n'
                obs_tn_html += '''
    </article>
'''
        obs_tn_html += '''
</section>
'''
        return obs_tn_html