Skip to content

Commit

Permalink
[Documentation] Fix command output and relative links
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Mar 6, 2021
1 parent 45d967f commit e4e0acd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
41 changes: 21 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ Usage
Command line
------------

usage: chat_downloader [-h] [--version] [--start_time START_TIME]
[--end_time END_TIME]
[--message_types MESSAGE_TYPES | --message_groups MESSAGE_GROUPS]
[--max_attempts MAX_ATTEMPTS]
[--retry_timeout RETRY_TIMEOUT]
[--max_messages MAX_MESSAGES]
[--inactivity_timeout INACTIVITY_TIMEOUT]
[--timeout TIMEOUT] [--format FORMAT]
[--format_file FORMAT_FILE] [--chat_type {live,top}]
[--ignore IGNORE]
[--message_receive_timeout MESSAGE_RECEIVE_TIMEOUT]
[--buffer_size BUFFER_SIZE] [--output OUTPUT]
[--overwrite] [--sort_keys] [--indent INDENT]
[--pause_on_debug | --exit_on_debug]
[--logging {none,debug,info,warning,error,critical} | --testing | --verbose | --quiet]
[--cookies COOKIES] [--proxy PROXY]
url
.. code:: console
usage: chat_downloader [-h] [--version] [--start_time START_TIME]
[--end_time END_TIME]
[--message_types MESSAGE_TYPES | --message_groups MESSAGE_GROUPS]
[--max_attempts MAX_ATTEMPTS]
[--retry_timeout RETRY_TIMEOUT]
[--max_messages MAX_MESSAGES]
[--inactivity_timeout INACTIVITY_TIMEOUT]
[--timeout TIMEOUT] [--format FORMAT]
[--format_file FORMAT_FILE] [--chat_type {live,top}]
[--ignore IGNORE]
[--message_receive_timeout MESSAGE_RECEIVE_TIMEOUT]
[--buffer_size BUFFER_SIZE] [--output OUTPUT]
[--overwrite] [--sort_keys] [--indent INDENT]
[--pause_on_debug | --exit_on_debug]
[--logging {none,debug,info,warning,error,critical} | --testing | --verbose | --quiet]
[--cookies COOKIES] [--proxy PROXY]
url
For example, to save messages from a livestream to a JSON file, you can use:
Expand All @@ -90,7 +91,7 @@ For example, to save messages from a livestream to a JSON file, you can use:
For a description of these options, as well as advanced command line use-cases and examples, consult the :ref:`Command Line Usage` page.
For a description of these options, as well as advanced command line use-cases and examples, consult the `Command Line Usage <https://chat-downloader.readthedocs.io/en/docs/cli.html#command-line-usage>`_ page.


Python
Expand All @@ -106,7 +107,7 @@ Python
chat.print_formatted(message) # print the formatted message
For advanced python use-cases and examples, consult the :ref:`Python Documentation`.
For advanced python use-cases and examples, consult the `Python Documentation <https://chat-downloader.readthedocs.io/en/docs/source/index.html#python-documentation>`_.


##########
Expand Down Expand Up @@ -139,7 +140,7 @@ Chat items/messages are parsed into JSON objects (a.k.a. dictionaries) and shoul
}
For an extensive, documented list of included fields, consult the :ref:`Chat Items` page.
For an extensive, documented list of included fields, consult the `Chat Items <https://chat-downloader.readthedocs.io/en/docs/index.html#chat-items>`_ page.

##########################
Frequently Asked Questions
Expand Down
35 changes: 30 additions & 5 deletions docs/generate_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import os
import subprocess

# Generate command line output
# Prepare README.rst for GitHub
# This includes:
# - Generating command line output
# - Fixing links

readme_path = 'README.rst'
if not os.path.exists(readme_path):
Expand All @@ -11,12 +14,34 @@

readme = open(readme_path).read()
cmd_regex = r'(\.+\s+program-output::\s+(.+)\s+:shell:)'

def replace(match):
padding = ' '*4
def replace_cmd_output(match):
cmd = match.group(2)
output = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
return output[0].decode()
text = output[0].decode()


return '.. code:: console\n' + ''.join(map(lambda x: padding + x, text.splitlines(True)))

substitution = re.sub(cmd_regex, replace_cmd_output, readme)


relative_link_regex = r'<a class="reference internal" href="(.*)"><span class="std std-ref">(.*)</span></a>'
# Get generated links
readme_html = open('_build/README.html').read()

relative_link_dict = {name: link for link, name in re.findall(relative_link_regex, readme_html)}

BASE_READTHEDOCS_URL = 'https://chat-downloader.readthedocs.io/en/latest/'
BASE_READTHEDOCS_URL = 'https://chat-downloader.readthedocs.io/en/docs/' # Testing


reference_regex = r':ref:`(.*)`'
def replace_reference_tag(match):
text = match.group(1)
url = relative_link_dict.get(text)
return '`{} <{}{}>`_'.format(text, BASE_READTHEDOCS_URL, url)

substitution = re.sub(cmd_regex, replace, readme)
substitution = re.sub(reference_regex, replace_reference_tag, substitution)

print(substitution)

0 comments on commit e4e0acd

Please sign in to comment.