[
  {
    "path": ".flake8",
    "content": "[flake8]\nmax-line-length = 120\n"
  },
  {
    "path": ".gitignore",
    "content": "/.mypy_cache\n/.venv\n/__pycache__\n/cache/*\n/tmp/*\n"
  },
  {
    "path": "LICENSE",
    "content": "                    GNU GENERAL PUBLIC LICENSE\n                       Version 2, June 1991\n\n Copyright (C) 1989, 1991 Free Software Foundation, Inc.,\n 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n                            Preamble\n\n  The licenses for most software are designed to take away your\nfreedom to share and change it.  By contrast, the GNU General Public\nLicense is intended to guarantee your freedom to share and change free\nsoftware--to make sure the software is free for all its users.  This\nGeneral Public License applies to most of the Free Software\nFoundation's software and to any other program whose authors commit to\nusing it.  (Some other Free Software Foundation software is covered by\nthe GNU Lesser General Public License instead.)  You can apply it to\nyour programs, too.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthis service if you wish), that you receive source code or can get it\nif you want it, that you can change the software or use pieces of it\nin new free programs; and that you know you can do these things.\n\n  To protect your rights, we need to make restrictions that forbid\nanyone to deny you these rights or to ask you to surrender the rights.\nThese restrictions translate to certain responsibilities for you if you\ndistribute copies of the software, or if you modify it.\n\n  For example, if you distribute copies of such a program, whether\ngratis or for a fee, you must give the recipients all the rights that\nyou have.  You must make sure that they, too, receive or can get the\nsource code.  And you must show them these terms so they know their\nrights.\n\n  We protect your rights with two steps: (1) copyright the software, and\n(2) offer you this license which gives you legal permission to copy,\ndistribute and/or modify the software.\n\n  Also, for each author's protection and ours, we want to make certain\nthat everyone understands that there is no warranty for this free\nsoftware.  If the software is modified by someone else and passed on, we\nwant its recipients to know that what they have is not the original, so\nthat any problems introduced by others will not reflect on the original\nauthors' reputations.\n\n  Finally, any free program is threatened constantly by software\npatents.  We wish to avoid the danger that redistributors of a free\nprogram will individually obtain patent licenses, in effect making the\nprogram proprietary.  To prevent this, we have made it clear that any\npatent must be licensed for everyone's free use or not licensed at all.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\n                    GNU GENERAL PUBLIC LICENSE\n   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n\n  0. This License applies to any program or other work which contains\na notice placed by the copyright holder saying it may be distributed\nunder the terms of this General Public License.  The \"Program\", below,\nrefers to any such program or work, and a \"work based on the Program\"\nmeans either the Program or any derivative work under copyright law:\nthat is to say, a work containing the Program or a portion of it,\neither verbatim or with modifications and/or translated into another\nlanguage.  (Hereinafter, translation is included without limitation in\nthe term \"modification\".)  Each licensee is addressed as \"you\".\n\nActivities other than copying, distribution and modification are not\ncovered by this License; they are outside its scope.  The act of\nrunning the Program is not restricted, and the output from the Program\nis covered only if its contents constitute a work based on the\nProgram (independent of having been made by running the Program).\nWhether that is true depends on what the Program does.\n\n  1. You may copy and distribute verbatim copies of the Program's\nsource code as you receive it, in any medium, provided that you\nconspicuously and appropriately publish on each copy an appropriate\ncopyright notice and disclaimer of warranty; keep intact all the\nnotices that refer to this License and to the absence of any warranty;\nand give any other recipients of the Program a copy of this License\nalong with the Program.\n\nYou may charge a fee for the physical act of transferring a copy, and\nyou may at your option offer warranty protection in exchange for a fee.\n\n  2. You may modify your copy or copies of the Program or any portion\nof it, thus forming a work based on the Program, and copy and\ndistribute such modifications or work under the terms of Section 1\nabove, provided that you also meet all of these conditions:\n\n    a) You must cause the modified files to carry prominent notices\n    stating that you changed the files and the date of any change.\n\n    b) You must cause any work that you distribute or publish, that in\n    whole or in part contains or is derived from the Program or any\n    part thereof, to be licensed as a whole at no charge to all third\n    parties under the terms of this License.\n\n    c) If the modified program normally reads commands interactively\n    when run, you must cause it, when started running for such\n    interactive use in the most ordinary way, to print or display an\n    announcement including an appropriate copyright notice and a\n    notice that there is no warranty (or else, saying that you provide\n    a warranty) and that users may redistribute the program under\n    these conditions, and telling the user how to view a copy of this\n    License.  (Exception: if the Program itself is interactive but\n    does not normally print such an announcement, your work based on\n    the Program is not required to print an announcement.)\n\nThese requirements apply to the modified work as a whole.  If\nidentifiable sections of that work are not derived from the Program,\nand can be reasonably considered independent and separate works in\nthemselves, then this License, and its terms, do not apply to those\nsections when you distribute them as separate works.  But when you\ndistribute the same sections as part of a whole which is a work based\non the Program, the distribution of the whole must be on the terms of\nthis License, whose permissions for other licensees extend to the\nentire whole, and thus to each and every part regardless of who wrote it.\n\nThus, it is not the intent of this section to claim rights or contest\nyour rights to work written entirely by you; rather, the intent is to\nexercise the right to control the distribution of derivative or\ncollective works based on the Program.\n\nIn addition, mere aggregation of another work not based on the Program\nwith the Program (or with a work based on the Program) on a volume of\na storage or distribution medium does not bring the other work under\nthe scope of this License.\n\n  3. You may copy and distribute the Program (or a work based on it,\nunder Section 2) in object code or executable form under the terms of\nSections 1 and 2 above provided that you also do one of the following:\n\n    a) Accompany it with the complete corresponding machine-readable\n    source code, which must be distributed under the terms of Sections\n    1 and 2 above on a medium customarily used for software interchange; or,\n\n    b) Accompany it with a written offer, valid for at least three\n    years, to give any third party, for a charge no more than your\n    cost of physically performing source distribution, a complete\n    machine-readable copy of the corresponding source code, to be\n    distributed under the terms of Sections 1 and 2 above on a medium\n    customarily used for software interchange; or,\n\n    c) Accompany it with the information you received as to the offer\n    to distribute corresponding source code.  (This alternative is\n    allowed only for noncommercial distribution and only if you\n    received the program in object code or executable form with such\n    an offer, in accord with Subsection b above.)\n\nThe source code for a work means the preferred form of the work for\nmaking modifications to it.  For an executable work, complete source\ncode means all the source code for all modules it contains, plus any\nassociated interface definition files, plus the scripts used to\ncontrol compilation and installation of the executable.  However, as a\nspecial exception, the source code distributed need not include\nanything that is normally distributed (in either source or binary\nform) with the major components (compiler, kernel, and so on) of the\noperating system on which the executable runs, unless that component\nitself accompanies the executable.\n\nIf distribution of executable or object code is made by offering\naccess to copy from a designated place, then offering equivalent\naccess to copy the source code from the same place counts as\ndistribution of the source code, even though third parties are not\ncompelled to copy the source along with the object code.\n\n  4. You may not copy, modify, sublicense, or distribute the Program\nexcept as expressly provided under this License.  Any attempt\notherwise to copy, modify, sublicense or distribute the Program is\nvoid, and will automatically terminate your rights under this License.\nHowever, parties who have received copies, or rights, from you under\nthis License will not have their licenses terminated so long as such\nparties remain in full compliance.\n\n  5. You are not required to accept this License, since you have not\nsigned it.  However, nothing else grants you permission to modify or\ndistribute the Program or its derivative works.  These actions are\nprohibited by law if you do not accept this License.  Therefore, by\nmodifying or distributing the Program (or any work based on the\nProgram), you indicate your acceptance of this License to do so, and\nall its terms and conditions for copying, distributing or modifying\nthe Program or works based on it.\n\n  6. Each time you redistribute the Program (or any work based on the\nProgram), the recipient automatically receives a license from the\noriginal licensor to copy, distribute or modify the Program subject to\nthese terms and conditions.  You may not impose any further\nrestrictions on the recipients' exercise of the rights granted herein.\nYou are not responsible for enforcing compliance by third parties to\nthis License.\n\n  7. If, as a consequence of a court judgment or allegation of patent\ninfringement or for any other reason (not limited to patent issues),\nconditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot\ndistribute so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you\nmay not distribute the Program at all.  For example, if a patent\nlicense would not permit royalty-free redistribution of the Program by\nall those who receive copies directly or indirectly through you, then\nthe only way you could satisfy both it and this License would be to\nrefrain entirely from distribution of the Program.\n\nIf any portion of this section is held invalid or unenforceable under\nany particular circumstance, the balance of the section is intended to\napply and the section as a whole is intended to apply in other\ncircumstances.\n\nIt is not the purpose of this section to induce you to infringe any\npatents or other property right claims or to contest validity of any\nsuch claims; this section has the sole purpose of protecting the\nintegrity of the free software distribution system, which is\nimplemented by public license practices.  Many people have made\ngenerous contributions to the wide range of software distributed\nthrough that system in reliance on consistent application of that\nsystem; it is up to the author/donor to decide if he or she is willing\nto distribute software through any other system and a licensee cannot\nimpose that choice.\n\nThis section is intended to make thoroughly clear what is believed to\nbe a consequence of the rest of this License.\n\n  8. If the distribution and/or use of the Program is restricted in\ncertain countries either by patents or by copyrighted interfaces, the\noriginal copyright holder who places the Program under this License\nmay add an explicit geographical distribution limitation excluding\nthose countries, so that distribution is permitted only in or among\ncountries not thus excluded.  In such case, this License incorporates\nthe limitation as if written in the body of this License.\n\n  9. The Free Software Foundation may publish revised and/or new versions\nof the General Public License from time to time.  Such new versions will\nbe similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\nEach version is given a distinguishing version number.  If the Program\nspecifies a version number of this License which applies to it and \"any\nlater version\", you have the option of following the terms and conditions\neither of that version or of any later version published by the Free\nSoftware Foundation.  If the Program does not specify a version number of\nthis License, you may choose any version ever published by the Free Software\nFoundation.\n\n  10. If you wish to incorporate parts of the Program into other free\nprograms whose distribution conditions are different, write to the author\nto ask for permission.  For software which is copyrighted by the Free\nSoftware Foundation, write to the Free Software Foundation; we sometimes\nmake exceptions for this.  Our decision will be guided by the two goals\nof preserving the free status of all derivatives of our free software and\nof promoting the sharing and reuse of software generally.\n\n                            NO WARRANTY\n\n  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\nFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\nOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\nPROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\nOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\nTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\nPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\nREPAIR OR CORRECTION.\n\n  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\nREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\nINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\nOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\nTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\nYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\nPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\nPOSSIBILITY OF SUCH DAMAGES.\n\n                     END OF TERMS AND CONDITIONS\n\n            How to Apply These Terms to Your New Programs\n\n  If you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\n  To do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nconvey the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    <one line to give the program's name and a brief idea of what it does.>\n    Copyright (C) <year>  <name of author>\n\n    This program is free software; you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation; either version 2 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License along\n    with this program; if not, write to the Free Software Foundation, Inc.,\n    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n\nAlso add information on how to contact you by electronic and paper mail.\n\nIf the program is interactive, make it output a short notice like this\nwhen it starts in an interactive mode:\n\n    Gnomovision version 69, Copyright (C) year name of author\n    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n    This is free software, and you are welcome to redistribute it\n    under certain conditions; type `show c' for details.\n\nThe hypothetical commands `show w' and `show c' should show the appropriate\nparts of the General Public License.  Of course, the commands you use may\nbe called something other than `show w' and `show c'; they could even be\nmouse-clicks or menu items--whatever suits your program.\n\nYou should also get your employer (if you work as a programmer) or your\nschool, if any, to sign a \"copyright disclaimer\" for the program, if\nnecessary.  Here is a sample; alter the names:\n\n  Yoyodyne, Inc., hereby disclaims all copyright interest in the program\n  `Gnomovision' (which makes passes at compilers) written by James Hacker.\n\n  <signature of Ty Coon>, 1 April 1989\n  Ty Coon, President of Vice\n\nThis General Public License does not permit incorporating your program into\nproprietary programs.  If your program is a subroutine library, you may\nconsider it more useful to permit linking proprietary applications with the\nlibrary.  If this is what you want to do, use the GNU Lesser General\nPublic License instead of this License.\n"
  },
  {
    "path": "Makefile",
    "content": "SCRIPTS = retronews.py tests.py\n\n.PHONY: venv-check\nvenv-check:\nifeq (\"$(VIRTUAL_ENV)\",\"\")\n\t@echo \"Venv is not active, run make venv-build and \\`make venv-activate\\`\"\n\t@echo\n\texit 1\nendif\n\nvenv-build:\n\tpython3 -m venv .venv\n\nvenv-activate:\n\t@echo \"source .venv/bin/activate\"\n\ninstall: venv-check\n\tpip install black isort flake8 mypy\n\nlint: venv-check\n\tblack $(SCRIPTS)\n\tisort $(SCRIPTS)\n\tflake8 $(SCRIPTS)\n\tmypy $(SCRIPTS)\n\ntest:\n\tpython3 tests.py\n"
  },
  {
    "path": "README.md",
    "content": "# retronews\n\nA Python script for browsing [Hacker News](https://news.ycombinator.com/)\nand [Lobsters](https://lobste.rs/) discussions with a nostalgic interface emulating\nclassical usenet and mail readers, like slrn and mutt.\n\nIt was primarily written so I could highlight interesting threads and keep\ntrack of read / unread messages (see [blog post](https://luke8086.dev/retronews.html)).\nThe UI showing one message at a time also encourages slower, more focused reading.\n\nOn Unix-like systems it only depends on Python 3.9. On Windows you may also need\nto install [windows-curses](https://pypi.org/project/windows-curses/).\n\nIt doesn't require installation. You can run it simply with:\n\n```bash\n$ curl -LO https://raw.githubusercontent.com/luke8086/retronews/main/retronews.py\n$ python3 ./retronews.py\n```\n\nPress `?` to see available keybindings.\n\n<img src=\"screenshot.png\" width=\"600\" />\n\n## Customization\n\nTo customize retronews without directly editing the script, you can put any valid\nPython code in `~/.retronewsrc.py` (or other location specified with `--rcfile`) to\nbe executed on startup. For example:\n\n```python\n# Ignore type warnings\nfrom typing import Any\nretronews: Any\n\n# Example: Custom key bindings\nretronews.KEY_BINDINGS[ord('a')] = lambda app: retronews.cmd_prev(app)\nretronews.KEY_BINDINGS[ord('z')] = lambda app: retronews.cmd_next(app)\n\n# Example: Custom colors\nretronews.COLORS['author'] = (retronews.curses.COLOR_RED, -1)\n```\n\n## Known issues and limitations\n\n- The reader is read-only, there are no plans to support voting and posting\n- Message formatting is not perfect, but works well enough most of the time\n- Detecting if threads contain unread responses works by only checking their\n  count, it's not reliable if any responses were deleted\n\n## Why not an NNTP gateway?\n\nNNTP doesn't support browsing threads by title (let alone paginated) and requesting\ntheir messages on demand. Clients need to fetch metadata of all available messages\nin all available threads in advance. Given the volume of messages on HN, synchronizing\nthem to the gateway is not practical. Even when attempted, some clients struggle\nwith the sheer number of messages in a single group.\n\n## Related projects\n\n- [HN Search @ Algolia](https://hn.algolia.com/about) - The underlying API used to retrieve messages\n- [nntpit](https://github.com/taviso/nntpit) - An NNTP gateway to reddit.com\n- [circumflex](https://github.com/bensadeh/circumflex) - Another, more advanced TUI for HN\n"
  },
  {
    "path": "pyproject.toml",
    "content": "[tool.isort]\nprofile = \"black\"\n\n[tool.black]\nline-length = 120\n"
  },
  {
    "path": "retronews.py",
    "content": "#!/usr/bin/env python3\n#\n# Copyright (c) luke8086\n#\n# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License version 2 as published by\n# the Free Software Foundation.\n#\n\nimport sys\n\nif sys.version_info < (3, 9):\n    sys.stderr.write(\"Python 3.9 or newer is required.\\n\")\n    sys.exit(1)\n\nimport argparse\nimport curses\nimport curses.textpad\nimport dataclasses\nimport html.parser\nimport json\nimport logging\nimport os\nimport re\nimport sqlite3\nimport traceback\nimport unicodedata\nimport urllib.request\nimport webbrowser\nfrom collections import defaultdict\nfrom datetime import datetime\nfrom functools import partial, reduce\nfrom textwrap import wrap\nfrom typing import (\n    TYPE_CHECKING,\n    Any,\n    Callable,\n    Generator,\n    Literal,\n    NewType,\n    Optional,\n    TypedDict,\n    TypeVar,\n    Union,\n    cast,\n    overload,\n)\n\nKEY_BINDINGS: dict[int, Callable[[\"AppState\"], None]] = {\n    ord(\"q\"): lambda app: cmd_quit(app),\n    ord(\"?\"): lambda app: cmd_help(app),\n    ord(\"\\n\"): lambda app: cmd_open(app),\n    ord(\" \"): lambda app: cmd_open(app),\n    ord(\"o\"): lambda app: cmd_show_links(app),\n    ord(\"x\"): lambda app: cmd_close(app),\n    ord(\"s\"): lambda app: cmd_star(app),\n    ord(\"S\"): lambda app: cmd_star_thread(app),\n    ord(\"u\"): lambda app: cmd_set_unread(app),\n    ord(\"D\"): lambda app: cmd_dump(app),\n    ord(\"r\"): lambda app: cmd_toggle_raw_mode(app),\n    ord(\"k\"): lambda app: cmd_up(app),\n    ord(\"j\"): lambda app: cmd_down(app),\n    ord(\"p\"): lambda app: cmd_prev(app),\n    ord(\"n\"): lambda app: cmd_next(app),\n    ord(\"N\"): lambda app: cmd_next_unread(app),\n    ord(\"P\"): lambda app: cmd_parent(app),\n    ord(\";\"): lambda app: cmd_mark_set(app),\n    ord(\",\"): lambda app: cmd_mark_jump(app),\n    ord(\"R\"): lambda app: cmd_reload_page(app),\n    ord(\"<\"): lambda app: cmd_load_prev_page(app),\n    ord(\">\"): lambda app: cmd_load_next_page(app),\n    ord(\"g\"): lambda app: cmd_load_page(app),\n    curses.KEY_UP: lambda app: cmd_prev(app),\n    curses.KEY_DOWN: lambda app: cmd_next(app),\n    curses.KEY_PPAGE: lambda app: cmd_page_up(app),\n    curses.KEY_NPAGE: lambda app: cmd_page_down(app),\n    curses.KEY_RESIZE: lambda app: cmd_resize(app),\n} | {ord(str(i)): lambda app, i=i: cmd_load_tab(app, i) for i in range(1, 10)}\n\nHELP_MENU = \"q:Quit  ?:Help  p:Prev  n:Next  N:Next-Unread  j:Down  k:Up  x:Close  s:Star\"\n\nHELP_SCREEN = \"\"\"\\\n  q                       Quit retronews\n  UP, DOWN                Go up / down by one message / pager line\n  PG UP, PG DOWN          Gp up / down by one page of messages / pager lines\n  p, n                    Go to previous / next message\n  N                       Go to next unread message\n  P                       Go to parent message\n  ; ,                     Set mark, jump to mark & swap (valid within thread)\n  RETURN, SPACE           Open selected message\n  x                       Close current message / thread\n  o                       Select link and open in browser\n  1 - 9                   Change group\n  R                       Refresh current page\n  < >                     Go to previous / next page\n  g                       Go to specific page\n  k j                     Scroll pager up / down by one line\n  s                       Star / unstar current message\n  S                       Star / unstar current thread\n  u                       Mark current message as unread\n  r                       Toggle raw HTML mode\n\nSee https://github.com/luke8086/retronews for more information.\"\"\"\n\nColor = Literal[\n    \"author\",\n    \"code\",\n    \"cursor\",\n    \"date\",\n    \"default\",\n    \"empty_pager_line\",\n    \"deleted_message_pager_line\",\n    \"menu\",\n    \"menu_active\",\n    \"nested_quote\",\n    \"quote\",\n    \"starred_subject\",\n    \"header_subject\",\n    \"tree\",\n    \"unread_comments\",\n    \"url\",\n]\n\nCOLORS: dict[Color, tuple[int, int]] = {\n    \"author\": (curses.COLOR_YELLOW, -1),\n    \"code\": (curses.COLOR_GREEN, -1),\n    \"cursor\": (curses.COLOR_BLACK, curses.COLOR_CYAN),\n    \"date\": (curses.COLOR_CYAN, -1),\n    \"default\": (curses.COLOR_WHITE, -1),\n    \"empty_pager_line\": (curses.COLOR_GREEN, -1),\n    \"deleted_message_pager_line\": (curses.COLOR_RED, -1),\n    \"menu\": (curses.COLOR_GREEN, curses.COLOR_BLUE),\n    \"menu_active\": (curses.COLOR_YELLOW, curses.COLOR_BLUE),\n    \"nested_quote\": (curses.COLOR_CYAN, -1),\n    \"quote\": (curses.COLOR_YELLOW, -1),\n    \"starred_subject\": (curses.COLOR_CYAN, -1),\n    \"header_subject\": (curses.COLOR_GREEN, -1),\n    \"tree\": (curses.COLOR_RED, -1),\n    \"unread_comments\": (curses.COLOR_GREEN, -1),\n    \"url\": (curses.COLOR_MAGENTA, -1),\n}\n\nREQUEST_TIMEOUT = 10\n\n# Recognize \">text\", \"> text\", \">>text\", \">> text\", etc.\nQUOTE_REX = re.compile(r\"^(> ?)+\")\n\n# Recognize \"[n] link\", \"[n]: link\", \"[n] - link\", etc.\nREFERENCE_REX = re.compile(r\"^\\[\\d+\\][ :-]*https?://[^ ]*$\")\n\n# Recognize http/https URLs\nURL_REX = re.compile(r\"(https?://[^\\s\\)\\\"<,]+[^\\s\\)\\\"<,\\.])\")\n\n# Recognize HN message URLs\nHN_URL_REX = re.compile(r\"^https://news\\.ycombinator\\.com/item\\?id=(\\d+)$\")\n\n# FIXME: Use TypeAlias after migrating to Python 3.10\nDB = NewType(\"DB\", \"sqlite3.Connection\")\n\nif TYPE_CHECKING:\n    from _curses import _CursesWindow\n\n    Window = _CursesWindow\nelse:\n    Window = Any\n\nT = TypeVar(\"T\")\n\n\nclass ExitException(Exception):\n    code: int\n    message: str\n\n    def __init__(self, code: int = 0, message: str = \"\"):\n        self.code = code\n        self.message = message\n\n        super().__init__(message)\n\n\n@dataclasses.dataclass(frozen=True)\nclass Provider:\n    fetch_thread: Callable[[str], \"Message\"]\n    fetch_threads_by_id: Callable[[list[str]], list[\"Message\"]]\n\n\nPROVIDERS: dict[str, Provider] = {\n    \"hn\": Provider(\n        fetch_thread=lambda msg_id: hn_fetch_thread(msg_id),\n        fetch_threads_by_id=lambda msg_ids: hn_fetch_threads_by_id(msg_ids),\n    ),\n    \"lb\": Provider(\n        fetch_thread=lambda msg_id: lb_fetch_thread(msg_id),\n        fetch_threads_by_id=lambda msg_ids: [lb_fetch_thread(x) for x in msg_ids],\n    ),\n}\n\n\n@dataclasses.dataclass(frozen=True)\nclass Group:\n    label: str\n    fetch: Callable[[DB, int], list[\"Message\"]]\n    page: int = 1\n\n\nGROUP_TABS: list[Group] = [\n    Group(label=\"Front HN\", fetch=lambda db, page: hn_fetch_threads(\"news\", page)),\n    Group(label=\"New HN\", fetch=lambda db, page: hn_fetch_new_threads(page)),\n    Group(label=\"Ask HN\", fetch=lambda db, page: hn_fetch_threads(\"ask\", page)),\n    Group(label=\"Show HN\", fetch=lambda db, page: hn_fetch_threads(\"show\", page)),\n    Group(label=\"Front LB\", fetch=lambda db, page: lb_fetch_threads(\"\", page)),\n    Group(label=\"New LB\", fetch=lambda db, page: lb_fetch_threads(\"newest\", page)),\n    Group(label=\"Starred\", fetch=lambda db, page: group_fetch_starred_threads(db, page)),\n]\n\n\n@dataclasses.dataclass\nclass MessageFlags:\n    read: bool = False\n    starred: bool = False\n\n\n@dataclasses.dataclass\nclass Message:\n    msg_id: str\n    thread_id: str\n    content_location: str\n    date: datetime\n    author: Optional[str]\n    title: str\n    body: Optional[str] = None\n    lines: list[str] = dataclasses.field(default_factory=list)\n    parent: Optional[\"Message\"] = None\n    children: Optional[list[\"Message\"]] = None\n    flags: MessageFlags = dataclasses.field(default_factory=MessageFlags)\n    read_comments: int = 0\n    total_comments: int = 0\n    index_position: int = 0\n    index_tree: str = \"\"\n\n    @property\n    def is_read(self) -> bool:\n        return self.flags.read\n\n    @property\n    def is_shown_as_read(self) -> bool:\n        # If the message is an unloaded thread, check if all comments are read\n        return self.read_comments >= self.total_comments if self.is_thread and self.children is None else self.is_read\n\n    @property\n    def is_thread(self) -> bool:\n        return self.msg_id == self.thread_id\n\n    @property\n    def is_deleted(self) -> bool:\n        return self.author is None\n\n\n@dataclasses.dataclass\nclass Layout:\n    lines: int = 0\n    cols: int = 0\n    top_menu_row: int = 0\n    index_start: int = 1\n    index_height: int = 0\n    middle_menu_row: Optional[int] = None\n    pager_start: Optional[int] = None\n    pager_height: Optional[int] = None\n    bottom_menu_row: int = 0\n    flash_menu_row: int = 0\n\n\n@dataclasses.dataclass\nclass AppState:\n    screen: Window\n    db: DB\n    group: Group\n    ascii: bool = False\n    monochrome: bool = False\n    colors: dict[Color, int] = dataclasses.field(default_factory=dict)\n    messages: list[Message] = dataclasses.field(default_factory=list)\n    messages_by_id: dict[str, Message] = dataclasses.field(default_factory=dict)\n    selected_message: Optional[Message] = None\n    marked_message_id: Optional[str] = None\n    layout: Layout = dataclasses.field(default_factory=Layout)\n    pager_visible: bool = False\n    pager_offset: int = 0\n    raw_mode: bool = False\n    flash: Optional[str] = None\n\n\nclass HNSearchHit(TypedDict):\n    objectID: int\n    author: Optional[str]\n    title: str\n    created_at_i: int\n    story_text: Optional[str]\n    url: Optional[str]\n    num_comments: int\n\n\nclass HNEntry(TypedDict):\n    author: Optional[str]\n    # FIXME: Recursive declarations are not yet supported in TypedDicts\n    children: list[Any]\n    created_at_i: int\n    id: int\n    parent_id: Optional[int]\n    text: Optional[str]\n    title: Optional[str]\n    url: Optional[str]\n\n\nclass LBThread(TypedDict):\n    short_id: str\n    short_id_url: str\n    created_at: str\n    title: str\n    url: str\n    description: str\n    submitter_user: str\n    comment_count: int\n    comments: Optional[list[Any]]\n\n\nclass LBComment(TypedDict):\n    short_id: str\n    created_at: str\n    url: str\n    commenting_user: str\n    parent_comment: Optional[str]\n\n\nHTML_BLOCK_TAGS = set((\"root\", \"p\", \"pre\", \"blockquote\", \"ul\", \"ol\", \"li\", \"hr\"))\nHTML_INLINE_TAGS = set((\"code\", \"a\", \"em\", \"strong\", \"b\", \"br\"))\nHTML_KNOWN_TAGS = HTML_BLOCK_TAGS.union(HTML_INLINE_TAGS)\nHTML_AUTOCLOSE_TAGS = set((\"hr\", \"br\"))\n\n\n@dataclasses.dataclass\nclass HTMLNode:\n    tag: str\n\n    parent: Optional[\"HTMLNode\"] = None\n\n    prev_sibling: Optional[\"HTMLNode\"] = None\n    next_sibling: Optional[\"HTMLNode\"] = None\n\n    first_child: Optional[\"HTMLNode\"] = None\n    last_child: Optional[\"HTMLNode\"] = None\n\n    attrs: dict[str, Optional[str]] = dataclasses.field(default_factory=dict)\n    text: str = \"\"\n    pre: bool = False\n\n\nclass HTMLParser(html.parser.HTMLParser):\n    root_node: HTMLNode\n    current_node: HTMLNode\n    pre_level = 0\n\n    def __init__(self):\n        super().__init__()\n\n        self.root_node = self.current_node = HTMLNode(tag=\"root\")\n\n    def handle_data(self, data: str) -> None:\n        if self.current_node.tag in HTML_AUTOCLOSE_TAGS:\n            self.handle_endtag(self.current_node.tag)\n\n        node = HTMLNode(tag=\"text\", text=data, pre=self.pre_level > 0)\n        html_node_append(self.current_node, node)\n\n    def handle_starttag(self, tag: str, attrs: list[tuple[str, Optional[str]]]) -> None:\n        if self.current_node.tag in HTML_AUTOCLOSE_TAGS:\n            self.handle_endtag(self.current_node.tag)\n\n        if tag not in HTML_KNOWN_TAGS:\n            return\n\n        if tag == \"pre\":\n            self.pre_level += 1\n\n        node = HTMLNode(tag=tag, attrs=dict(attrs), pre=self.pre_level > 0)\n        html_node_append(self.current_node, node)\n        self.current_node = node\n\n    def handle_endtag(self, tag: str) -> None:\n        if tag not in HTML_KNOWN_TAGS:\n            return\n\n        if tag == \"pre\":\n            self.pre_level = max(0, self.pre_level - 1)\n\n        while True:\n            node = self.current_node\n\n            if node.parent is None:\n                break\n\n            self.current_node = node.parent\n\n            if node.tag == tag:\n                break\n\n\ndef text_wrap(text: str, width=70) -> str:\n    if len(text) == 0:\n        # Preserve empty lines\n        return \"\"\n\n    if text.startswith(\"  \"):\n        # Preserve code indentation\n        return text\n\n    if REFERENCE_REX.match(text):\n        # Keep reference numbers with long links in the same line\n        return text\n\n    indent = \"\"\n\n    if (match := QUOTE_REX.match(text)) is not None:\n        # Preserve quotation symbols in subsequent lines\n        indent = match[0]\n\n    lines = wrap(text, width, subsequent_indent=indent, break_on_hyphens=False, break_long_words=False)\n    lines = [line.rstrip() for line in lines]\n\n    return \"\\n\".join(lines)\n\n\ndef text_clean(text: str, ascii: bool = False) -> str:\n    \"\"\"Cleanup text for rendering, currently only removes non-ascii characters in ascii mode\"\"\"\n\n    if ascii:\n        text = text.encode(\"ascii\", \"replace\").decode(\"ascii\")\n\n    return text\n\n\ndef text_unindent(text: str) -> str:\n    lines = text.split(\"\\n\")\n\n    while all(line.startswith(\" \") or line == \"\" for line in lines):\n        lines = [line[1:] for line in lines]\n\n    return \"\\n\".join(lines)\n\n\ndef text_sanitize(text: Optional[str]) -> str:\n    # For safety, remove any control characters except for \\n and \\t\n    # At least on HN some messages contain \\x00 characters\n\n    text = text or \"\"\n    allowed_cc = set((\"\\n\", \"\\t\"))\n    chars = (c for c in text if c in allowed_cc or unicodedata.category(c) != \"Cc\")\n    text = \"\".join(chars)\n\n    return text\n\n\ndef text_split_urls(text: str) -> list[str]:\n    return [p for p in URL_REX.split(text) if p != \"\"]\n\n\ndef html_node_children(parent: HTMLNode) -> list[HTMLNode]:\n    ret: list[HTMLNode] = []\n    node = parent.first_child\n\n    while node is not None:\n        ret.append(node)\n        node = node.next_sibling\n\n    return ret\n\n\ndef html_node_append(parent: HTMLNode, child: HTMLNode) -> None:\n    if parent.first_child is None:\n        parent.first_child = child\n\n    if parent.last_child is not None:\n        parent.last_child.next_sibling = child\n        child.prev_sibling = parent.last_child\n\n    parent.last_child = child\n    child.parent = parent\n\n\ndef html_node_unlink(node: HTMLNode) -> None:\n    if node.prev_sibling:\n        node.prev_sibling.next_sibling = node.next_sibling\n\n    if node.next_sibling:\n        node.next_sibling.prev_sibling = node.prev_sibling\n\n    if node.parent and node.parent.first_child is node:\n        node.parent.first_child = node.next_sibling\n\n    if node.parent and node.parent.last_child is node:\n        node.parent.last_child = node.prev_sibling\n\n    node.parent = node.prev_sibling = node.next_sibling = None\n\n\ndef html_node_dump(node: HTMLNode) -> str:\n    lines = []\n    lines.append(f\"{node.tag} {repr(node.attrs)}\")\n\n    for child in html_node_children(node):\n        if child.tag == \"text\":\n            lines.append(\"  text \" + repr(child.text))\n        else:\n            lines += [\"  \" + line for line in html_node_dump(child).split(\"\\n\")]\n\n    return \"\\n\".join(lines)\n\n\ndef html_node_trim_whitespace(node: HTMLNode) -> None:\n    if node.pre:\n        node.text = node.text.rstrip(\"\\r\\n\\t \")\n        return\n\n    text = node.text.strip(\"\\r\\n\\t \")\n    text = re.sub(r\"[\\r\\n\\t ]+\", \" \", text)\n    text = text.replace(\"\\x00\", \"\\n\")\n    text = re.sub(r\" *\\n *\", \"\\n\", text)\n\n    node.text = text\n\n\ndef html_node_process_inline(node: HTMLNode, inline=False) -> str:\n    \"\"\"Traverse tree flattening all inline nodes into text nodes\"\"\"\n\n    if node.tag not in HTML_BLOCK_TAGS:\n        # Never disable inline if already enabled\n        inline = True\n\n    text = \"\".join(html_node_process_inline(c, inline) for c in html_node_children(node))\n\n    if not inline:\n        return \"\"\n\n    if node.tag == \"text\":\n        text = node.text\n    elif node.tag == \"br\":\n        text = \"\\x00\"\n    elif node.tag == \"em\" or node.tag == \"i\":\n        text = f\"/{text}/\"\n    elif node.tag == \"strong\" or node.tag == \"b\":\n        text = f\"*{text}*\"\n    elif node.tag == \"code\" and not node.pre:\n        text = f\"`{text}`\"\n    elif node.tag == \"a\":\n        href = node.attrs.get(\"href\", \"\") or \"\"\n        if text.endswith(\"...\") and href.startswith(text[:-3]):\n            # Workaround for link formatting on HN\n            text = href\n        elif text != href:\n            text = f\"{text} {href}\"\n\n    node.tag = \"text\"\n    node.text = text\n    node.first_child = node.last_child = None\n\n    return text\n\n\ndef html_node_process_text(node: HTMLNode):\n    \"\"\"Traverse tree merging, trimming and pruning text nodes\"\"\"\n\n    for child in html_node_children(node):\n        html_node_process_text(child)\n\n    # Merge adjacent text nodes\n    for child in html_node_children(node):\n        prev = child.prev_sibling\n        if child.tag == \"text\" and prev is not None and prev.tag == \"text\" and child.pre == prev.pre:\n            child.text = prev.text + child.text\n            html_node_unlink(prev)\n\n    # Trim whitespace from text nodes\n    for child in html_node_children(node):\n        if child.tag == \"text\":\n            html_node_trim_whitespace(child)\n\n    # Remove empty text nodes\n    for child in html_node_children(node):\n        if child.tag == \"text\" and child.text == \"\":\n            html_node_unlink(child)\n\n\ndef html_node_render_block(node: HTMLNode, width=70) -> str:\n    if node.tag == \"blockquote\" or node.tag == \"li\" or node.tag == \"pre\":\n        width -= 2\n\n    parts = []\n\n    if node.tag == \"hr\":\n        parts.append(\"-\" * width)\n\n    for child in html_node_children(node):\n        if child.tag == \"text\" and child.pre:\n            parts.append(child.text)\n\n        elif child.tag == \"text\" and not child.pre:\n            subparts = [text_wrap(p, width) for p in child.text.split(\"\\n\")]\n            parts.append(\"\\n\".join(subparts))\n\n        else:\n            parts.append(html_node_render_block(child, width))\n\n        if child.next_sibling is not None and child.tag != \"li\":\n            parts.append(\"\")\n\n    text = \"\\n\".join(parts)\n\n    if node.tag == \"blockquote\":\n        text = \"\\n\".join((\">\" if line.startswith(\"> \") else \"> \") + line for line in text.split(\"\\n\"))\n    elif node.tag == \"pre\":\n        text = text_unindent(text)\n        text = \"\\n\".join(\"| \" + line for line in text.split(\"\\n\"))\n    elif node.tag == \"li\":\n        lines = text.split(\"\\n\")\n        lines = [(\"- \" + lines[0]).rstrip()] + [(\"  \" + line).rstrip() for line in lines[1:]]\n        text = \"\\n\".join(lines)\n\n    return text\n\n\ndef html_render(html: str) -> str:\n    # This renderer works well for HN messages because their markup is simple, and it can do\n    # some custom optimizations, like expanding ellipsis-shortened links, preserving quote\n    # symbols in wrapped lines, and preventing references with long urls from being broken\n    # into separate lines. For other backends it may make more sense to use an external app\n    # (links, w3m, etc)\n\n    html = text_sanitize(html)\n\n    parser = HTMLParser()\n    parser.feed(html)\n    parser.close()\n\n    node = parser.root_node\n\n    log_sep = \"\\n\" + \"-\" * 80 + \"\\n\"\n    logging.debug(f\"Initial HTML tree{log_sep}{html_node_dump(node)}{log_sep}\")\n\n    html_node_process_inline(node)\n    html_node_process_text(node)\n\n    logging.debug(f\"Processed HTML tree{log_sep}{html_node_dump(node)}{log_sep}\")\n\n    return html_node_render_block(node)\n\n\ndef fetch(url: str) -> str:\n    logging.debug(f\"Fetching '{url}'...\")\n\n    headers = {\"User-Agent\": \"retronews\"}\n    req = urllib.request.Request(url, headers=headers)\n    resp = urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT).read().decode()\n\n    return resp\n\n\n@overload\ndef list_get(lst: list[T], index: int, default: T) -> T: ...\n\n\n@overload\ndef list_get(lst: list[T], index: int, default: Optional[T] = None) -> Optional[T]: ...\n\n\ndef list_get(lst, index, default=None):\n    return lst[index] if 0 <= index < len(lst) else default\n\n\ndef list_chunk(lst: list[T], size: int) -> list[list[T]]:\n    # Flake8 conflicts with Black here - https://github.com/PyCQA/pycodestyle/issues/373\n    return [lst[i : i + size] for i in range(0, len(lst), size)]  # noqa: E203\n\n\ndef cmd_quit(_: AppState):\n    raise ExitException()\n\n\ndef cmd_help(app: AppState):\n    app_show_help_screen(app)\n\n\ndef cmd_show_links(app: AppState):\n    app_show_links_screen(app)\n\n\ndef cmd_up(app: AppState) -> None:\n    cmd_pager_up(app) if app.pager_visible else cmd_prev(app)\n\n\ndef cmd_down(app: AppState) -> None:\n    cmd_pager_down(app) if app.pager_visible else cmd_next(app)\n\n\ndef cmd_prev(app: AppState) -> None:\n    pos = app.selected_message.index_position - 1 if app.selected_message else 0\n    app_select_message(app, list_get(app.messages, pos, app.selected_message))\n\n\ndef cmd_next(app: AppState) -> None:\n    pos = app.selected_message.index_position + 1 if app.selected_message else 0\n    app_select_message(app, list_get(app.messages, pos, app.selected_message))\n\n\ndef cmd_next_unread(app: AppState) -> None:\n    pos = app.selected_message.index_position + 1 if app.selected_message else 0\n    message = next((msg for msg in app.messages[pos:] if not msg.is_shown_as_read), None)\n    if message is not None:\n        app_select_message(app, message)\n\n\ndef cmd_next_sibling(app: AppState) -> None:\n    if (msg := app.selected_message) and (parent_msg := msg.parent) and parent_msg.children:\n        # find the next sibling\n        try:\n            idx = parent_msg.children.index(msg)\n            if idx < len(parent_msg.children):\n                app_select_message(app, parent_msg.children[idx + 1])\n        except IndexError:\n            pass\n\n\ndef cmd_prev_sibling(app: AppState) -> None:\n    if (msg := app.selected_message) and (parent_msg := msg.parent) and parent_msg.children:\n        # find the previous sibling\n        try:\n            idx = parent_msg.children.index(msg)\n            if idx > 0:\n                app_select_message(app, parent_msg.children[idx - 1])\n        except IndexError:\n            pass\n\n\ndef cmd_mark_thread_as_read(app: AppState) -> None:\n    # recursively mark us and all children as read\n    # then jump to the next sibling\n    def iterate(message: Message):\n        if message.children:\n            for child in message.children:\n                child.flags.read = True\n                db_save_message(app.db, child)\n                iterate(child)\n\n    if (msg := app.selected_message) is not None:\n        iterate(msg)\n        # jump to the next sibling\n        cmd_next_sibling(app)\n\n\ndef cmd_parent(app: AppState) -> None:\n    if (msg := app.selected_message) is not None and (parent_msg := msg.parent) is not None:\n        app_select_message(app, parent_msg)\n\n\ndef cmd_mark_set(app: AppState) -> None:\n    if (msg := app.selected_message) is not None:\n        app.marked_message_id = msg.msg_id\n    app_show_flash(app, \"Mark set\")\n\n\ndef cmd_mark_jump(app: AppState) -> None:\n    marked_msg = app.messages_by_id.get(app.marked_message_id) if app.marked_message_id else None\n    cmd_mark_set(app)\n    if marked_msg is not None:\n        app_select_message(app, marked_msg)\n    app_show_flash(app, \"Mark swapped\")\n\n\ndef cmd_pager_up(app: AppState) -> None:\n    app.pager_offset = max(0, app.pager_offset - 1)\n\n\ndef cmd_pager_down(app: AppState) -> None:\n    if app.selected_message is not None and app.layout.pager_height is not None:\n        app.pager_offset = min(app.pager_offset + 1, max(0, len(app.selected_message.lines) - app.layout.pager_height))\n\n\ndef cmd_page_up(app: AppState) -> None:\n    cmd_pager_page_up(app) if app.pager_visible else cmd_index_page_up(app)\n\n\ndef cmd_page_down(app: AppState) -> None:\n    cmd_pager_page_down(app) if app.pager_visible else cmd_index_page_down(app)\n\n\ndef cmd_index_page_up(app: AppState) -> None:\n    pos = app.selected_message.index_position - app.layout.index_height if app.selected_message else 0\n    pos = max(pos, 0)\n    app_select_message(app, list_get(app.messages, pos, app.selected_message))\n\n\ndef cmd_index_page_down(app: AppState) -> None:\n    pos = app.selected_message.index_position + app.layout.index_height if app.selected_message else 0\n    pos = min(pos, len(app.messages) - 1)\n    app_select_message(app, list_get(app.messages, pos, app.selected_message))\n\n\ndef cmd_pager_page_up(app: AppState) -> None:\n    if app.layout.pager_height is not None:\n        app.pager_offset = max(0, app.pager_offset - app.layout.pager_height)\n\n\ndef cmd_pager_page_down(app: AppState) -> None:\n    if (message := app.selected_message) is not None and (pager_height := app.layout.pager_height) is not None:\n        app.pager_offset = min(app.pager_offset + pager_height, max(0, len(message.lines) - pager_height))\n\n\ndef cmd_load_tab(app: AppState, tab: int) -> None:\n    if group := list_get(GROUP_TABS, tab - 1):\n        app_load_group(app, group)\n\n\ndef cmd_reload_page(app: AppState) -> None:\n    app_load_group(app, app.group)\n\n\ndef cmd_load_prev_page(app: AppState) -> None:\n    app_load_group(app, group_advance_page(app.group, -1))\n\n\ndef cmd_load_next_page(app: AppState) -> None:\n    app_load_group(app, group_advance_page(app.group, 1))\n\n\ndef cmd_load_page(app: AppState) -> None:\n    user_input = app_prompt(app, \"Go to page (empty to cancel): \")\n\n    if user_input.isnumeric() and (page := int(user_input)) >= 1:\n        app_load_group(app, group_set_page(app.group, page))\n    elif len(user_input) > 0:\n        app_show_flash(app, \"Invalid page number\")\n\n\ndef cmd_open(app: AppState) -> None:\n    if (msg := app.selected_message) is None:\n        return\n\n    if msg.is_thread:\n        app_open_thread(app, msg)\n    else:\n        app_select_message(app, msg, show_pager=True)\n\n\ndef cmd_close(app: AppState) -> None:\n    if app.pager_visible:\n        app.pager_visible = False\n    else:\n        app_close_thread(app)\n\n\ndef cmd_star(app: AppState) -> None:\n    if (msg := app.selected_message) is not None:\n        msg.flags.starred = not msg.flags.starred\n        db_save_message(app.db, msg)\n        cmd_next(app)\n\n\ndef cmd_star_thread(app: AppState) -> None:\n    if (msg := app.selected_message) is None:\n        return\n\n    if (thread_msg := app.messages_by_id.get(msg.thread_id)) is None:\n        return\n\n    thread_msg.flags.starred = not thread_msg.flags.starred\n    db_save_message(app.db, thread_msg)\n    cmd_next(app)\n\n\ndef cmd_set_unread(app: AppState) -> None:\n    if (msg := app.selected_message) is not None:\n        msg.flags.read = False\n        db_save_message(app.db, msg)\n        cmd_next(app)\n\n\ndef cmd_dump(app: AppState) -> None:\n    if (msg := app.selected_message) is None:\n        return\n\n    filename = f\"{msg.msg_id}.html\"\n\n    with open(filename, \"w\") as fp:\n        fp.write(msg.body or \"\")\n\n    app_show_flash(app, f\"Message body dumped to {filename}\")\n\n\ndef cmd_toggle_raw_mode(app: AppState) -> None:\n    app.raw_mode = not app.raw_mode\n    app_select_message(app, app.selected_message)\n\n\ndef cmd_resize(app: AppState) -> None:\n    app_refresh_message(app)\n\n\ndef cmd_unknown(app: AppState) -> None:\n    app.flash = \"Unknown key\"\n\n\ndef db_init(path: str) -> DB:\n    path = os.path.expanduser(path)\n    create_table_sql = \"\"\"\n        CREATE TABLE IF NOT EXISTS messages (\n            msg_id TEXT NOT NULL PRIMARY KEY,\n            thread_id TEXT NOT NULL,\n            date INTEGER NOT NULL,\n            flags JSON NOT NULL\n        );\n\n        CREATE INDEX IF NOT EXISTS messages_starred_date ON messages (\n            JSON_EXTRACT(flags, '$.starred'),\n            date\n        );\n    \"\"\"\n\n    db = sqlite3.connect(path)\n    db.row_factory = sqlite3.Row\n    db.executescript(create_table_sql)\n    db.commit()\n\n    return cast(DB, db)\n\n\ndef db_save_message(db: DB, message: Message) -> None:\n    sql = \"\"\"INSERT OR REPLACE INTO messages (msg_id, thread_id, date, flags) VALUES (?, ?, ?, ?)\"\"\"\n    date = int(message.date.timestamp())\n    flags_json = json.dumps(dataclasses.asdict(message.flags))\n    db.execute(sql, (message.msg_id, message.thread_id, date, flags_json))\n    db.commit()\n\n\ndef db_load_message_flags(db: DB, messages_by_id: dict[str, Message]) -> None:\n    message_ids = list(messages_by_id.keys())\n    sql = f\"SELECT * FROM messages WHERE msg_id IN ({','.join('?' for _ in message_ids)})\"\n\n    for row in db.execute(sql, message_ids):\n        flags = json.loads(row[\"flags\"])\n        messages_by_id[row[\"msg_id\"]].flags = MessageFlags(**flags)\n\n\ndef db_load_read_comments(db: DB, messages_by_id: dict[str, Message]) -> None:\n    threads_by_id = {msg.msg_id: msg for msg in messages_by_id.values() if msg.is_thread}\n    thread_ids = list(threads_by_id.keys())\n\n    sql = f\"\"\"\n        SELECT thread_id, COUNT(*) AS count\n        FROM messages\n        WHERE thread_id IN ({','.join('?' for _ in thread_ids)}) AND JSON_EXTRACT(flags, '$.read')\n        GROUP BY thread_id\n    \"\"\"\n\n    for row in db.execute(sql, thread_ids):\n        threads_by_id[row[\"thread_id\"]].read_comments = row[\"count\"]\n\n\ndef db_load_starred_thread_ids(db: DB, page: int = 1) -> list[str]:\n    page_size = 30\n    offset = (page - 1) * page_size\n    sql = \"\"\"\n        SELECT thread_id\n        FROM messages\n        WHERE JSON_EXTRACT(flags, '$.starred')\n        GROUP BY thread_id\n        ORDER BY date DESC\n        LIMIT ?\n        OFFSET ?\n    \"\"\"\n\n    return [row[\"thread_id\"] for row in db.execute(sql, (page_size, offset))]\n\n\ndef msg_flatten_thread(\n    msg: Message, prefix: str = \"\", is_last_child: bool = False, ascii: bool = False\n) -> Generator[Message, None, None]:\n    blcorner = \"'-\" if ascii else \"└─\"\n    ltee = \"|-\" if ascii else \"├─\"\n    vline = \"| \" if ascii else \"│ \"\n\n    msg.index_tree = \"\" if msg.is_thread else f\"{prefix}{blcorner if is_last_child else ltee}> \"\n    yield msg\n\n    children = msg.children or []\n\n    child_count = len(children)\n    child_prefix = \"\" if msg.is_thread else f\"{prefix}{'  ' if is_last_child else vline}\"\n\n    for i, child_node in enumerate(children):\n        child_is_last = i == child_count - 1\n        for child in msg_flatten_thread(child_node, prefix=child_prefix, is_last_child=child_is_last, ascii=ascii):\n            yield child\n\n\ndef msg_build_raw_lines(msg: Message) -> list[str]:\n    text = text_sanitize(msg.body)\n\n    # Unescape selected entities for better readability\n    repl = {\"&#x2F;\": \"/\", \"&#x27;\": \"'\", \"&quot;\": '\"'}\n    for k, v in repl.items():\n        text = text.replace(k, v)\n\n    return reduce(lambda acc, line: acc + wrap(line, width=120, replace_whitespace=False), text.split(\"\\n\"), [])\n\n\ndef msg_build_lines(msg: Message) -> list[str]:\n    lines = [\n        f\"Content-Location: {msg.content_location}\",\n        f\"Date: {msg.date.strftime('%Y-%m-%d %H:%M')}\",\n        f\"From: {msg.author or '<unknown>'}\",\n        f\"Subject: {msg.title}\",\n        \"\",\n    ]\n\n    lines += html_render(msg.body or \"\").split(\"\\n\") if not msg.is_deleted else [\"<deleted>\"]\n\n    return lines\n\n\ndef msg_unload(msg: Message) -> Message:\n    msg.children = None\n    msg.body = None\n    return msg\n\n\ndef hn_parse_search_hit(hit: HNSearchHit) -> Message:\n    return Message(\n        msg_id=f\"{hit['objectID']}@hn\",\n        thread_id=f\"{hit['objectID']}@hn\",\n        content_location=f\"https://news.ycombinator.com/item?id={hit['objectID']}\",\n        date=datetime.fromtimestamp(hit[\"created_at_i\"]),\n        author=hit[\"author\"],\n        title=html.unescape(hit[\"title\"]),\n        total_comments=(hit[\"num_comments\"] or 0) + 1,\n    )\n\n\ndef hn_parse_entry(entry: HNEntry, thread_id: str = \"\", parent: Optional[Message] = None) -> Message:\n    thread_id = thread_id or str(entry[\"id\"])\n\n    my_title = html.unescape(entry[\"title\"]) if entry[\"title\"] else None\n\n    parent_title = parent.title if parent else \"\"\n    parent_title = parent_title if parent_title.startswith(\"Re: \") else f\"Re: {parent_title}\"\n\n    body = f\"<p>{entry['url']}</p>\" if entry[\"url\"] else \"\"\n    body = f\"{body}{entry['text']}\" if entry[\"text\"] else body\n\n    msg = Message(\n        msg_id=f\"{entry['id']}@hn\",\n        thread_id=f\"{thread_id}@hn\",\n        content_location=f\"https://news.ycombinator.com/item?id={entry['id']}\",\n        date=datetime.fromtimestamp(entry[\"created_at_i\"]),\n        author=entry[\"author\"],\n        title=my_title or parent_title,\n        body=body,\n        parent=parent,\n    )\n\n    msg.children = [hn_parse_entry(child, thread_id, msg) for child in entry[\"children\"]]\n\n    return msg\n\n\ndef hn_fetch_threads_by_id(thread_ids: list[str]) -> list[Message]:\n    story_tags = \",\".join(f\"story_{x}\" for x in thread_ids)\n    url = f\"https://hn.algolia.com/api/v1/search_by_date?hitsPerPage={len(thread_ids)}&tags=story,({story_tags})\"\n    hits = json.loads(fetch(url))[\"hits\"]\n    hits_by_id = {hit[\"objectID\"]: hit for hit in hits}\n    threads = [hn_parse_search_hit(hits_by_id[tid]) for tid in thread_ids if tid in hits_by_id]\n\n    return threads\n\n\ndef hn_fetch_threads(group: str = \"news\", page: int = 1) -> list[Message]:\n    rex = re.compile(r'href=\"item\\?id=(\\d+)\"')\n\n    html = fetch(f\"https://news.ycombinator.com/{group}?p={page}\")\n    thread_ids = list(dict.fromkeys(match.group(1) for match in rex.finditer(html)))\n\n    return hn_fetch_threads_by_id(thread_ids)\n\n\ndef hn_fetch_new_threads(page: int = 1) -> list[Message]:\n    url = f\"https://hn.algolia.com/api/v1/search_by_date?tags=story&hitsPerPage=30&page={page}\"\n    hits = json.loads(fetch(url))[\"hits\"]\n\n    return [hn_parse_search_hit(hit) for hit in hits]\n\n\ndef hn_fetch_thread(entry_id: Union[str, int]) -> Message:\n    resp = fetch(f\"http://hn.algolia.com/api/v1/items/{entry_id}\")\n    entry: HNEntry = json.loads(resp)\n    return hn_parse_entry(entry)\n\n\ndef lb_parse_thread(thread: LBThread) -> Message:\n    comments = {}\n\n    thread_body = f\"{thread['description']}\"\n    thread_body = f\"<p>{thread['url']}</p>{thread_body}\" if thread[\"url\"] else thread_body\n\n    ret = Message(\n        msg_id=f\"{thread['short_id']}@lb\",\n        thread_id=f\"{thread['short_id']}@lb\",\n        content_location=thread[\"short_id_url\"],\n        date=datetime.fromtimestamp(datetime.fromisoformat(thread[\"created_at\"]).timestamp()),\n        author=thread[\"submitter_user\"],\n        title=thread[\"title\"],\n        body=thread_body,\n        children=None if thread.get(\"comments\") is None else [],\n        total_comments=thread[\"comment_count\"] + 1,\n    )\n\n    for comment in thread.get(\"comments\", []) or []:\n        comments[comment[\"short_id\"]] = Message(\n            msg_id=f\"{comment['short_id']}@lb\",\n            thread_id=f\"{thread['short_id']}@lb\",\n            content_location=comment[\"url\"],\n            date=datetime.fromtimestamp(datetime.fromisoformat(comment[\"created_at\"]).timestamp()),\n            author=comment[\"commenting_user\"],\n            title=f\"Re: {thread['title']}\",\n            body=comment[\"comment\"],\n            children=[],\n        )\n\n    for comment in thread.get(\"comments\", []) or []:\n        msg = comments[comment[\"short_id\"]]\n        parent_msg = comments[comment[\"parent_comment\"]] if comment[\"parent_comment\"] else ret\n        msg.parent = parent_msg\n        if parent_msg.children is not None:\n            parent_msg.children.append(msg)\n\n    return ret\n\n\ndef lb_fetch_threads(group: str = \"\", page: int = 1) -> list[Message]:\n    group_path = f\"{group}/\" if group else \"\"\n    resp = fetch(f\"https://lobste.rs/{group_path}page/{page}.json\")\n    threads: list[LBThread] = json.loads(resp)\n\n    return [lb_parse_thread(thread) for thread in threads]\n\n\ndef lb_fetch_thread(entry_id: str) -> Message:\n    resp = fetch(f\"https://lobste.rs/s/{entry_id}.json\")\n    thread: LBThread = json.loads(resp)\n\n    return lb_parse_thread(thread)\n\n\ndef group_set_page(group: Group, page: int) -> Group:\n    return dataclasses.replace(group, page=page)\n\n\ndef group_advance_page(group: Group, offset: int = 1) -> Group:\n    return group_set_page(group, page=max(1, group.page + offset))\n\n\ndef group_fetch_starred_threads(db: DB, page: int = 1) -> list[Message]:\n    thread_ids = db_load_starred_thread_ids(db, page)\n    threads_by_provider_id: dict[str, list[str]] = {}\n    threads = []\n\n    for source_id, provider_id in (t.split(\"@\") for t in thread_ids):\n        threads_by_provider_id.setdefault(provider_id, list()).append(source_id)\n\n    for provider_id, thread_ids in threads_by_provider_id.items():\n        provider = PROVIDERS[provider_id]\n        threads += provider.fetch_threads_by_id(thread_ids)\n\n    threads.sort(key=lambda x: x.date, reverse=True)\n\n    return threads\n\n\ndef group_fetch_thread(thread_id: str) -> Message:\n    (msg_id, provider_id) = thread_id.split(\"@\")\n    provider = PROVIDERS[provider_id]\n\n    return provider.fetch_thread(msg_id)\n\n\ndef group_for_msg_url(url: str) -> Group:\n    if (match := HN_URL_REX.match(url)) is not None:\n        msg_id = match[1]\n        return Group(label=\"\", fetch=lambda *x: hn_fetch_threads_by_id([msg_id]))\n\n    msg = \"Unknown URL, available patterns: \\n\" + \"\\n\".join(f\"- {r.pattern}\" for r in [HN_URL_REX])\n    raise ExitException(1, msg)\n\n\ndef app_safe_run(app: AppState, fn: Callable[[], T], flash: Optional[str]) -> Optional[T]:\n    if flash is not None:\n        app_show_flash(app, flash)\n\n    ret = None\n\n    try:\n        ret = fn()\n    except Exception as e:\n        logging.debug(\"\\n\".join(traceback.format_exception(e)))\n        app_show_flash(app, f\"Error: {e}\")\n    else:\n        if flash is not None:\n            app_show_flash(app, None)\n\n    return ret\n\n\ndef app_refresh_message(app: AppState) -> None:\n    app.pager_offset = 0\n\n    # Converting html to lines lazily on render for easier debugging\n    if (msg := app.selected_message) is not None:\n        msg.lines = msg_build_raw_lines(msg) if app.raw_mode else msg_build_lines(msg)\n\n\ndef app_select_message(app: AppState, message: Optional[Message], show_pager: bool = False) -> None:\n    app.selected_message = message\n\n    app_refresh_message(app)\n\n    if message is None or message.body is None:\n        app.pager_visible = False\n        return\n\n    if show_pager:\n        app.pager_visible = True\n\n    if app.pager_visible:\n        message.flags.read = True\n        db_save_message(app.db, message)\n        db_load_read_comments(app.db, {message.thread_id: app.messages_by_id[message.thread_id]})\n\n\ndef app_load_messages(\n    app: AppState, messages: list[Message], selected_message_id: Optional[str] = None, show_pager: bool = False\n) -> None:\n    if selected_message_id is None and app.selected_message is not None:\n        selected_message_id = app.selected_message.msg_id\n\n    selected_message = None\n\n    for i, message in enumerate(messages):\n        message.index_position = i\n\n        if message.msg_id == selected_message_id:\n            selected_message = message\n\n    if selected_message is None and len(messages) > 0:\n        selected_message = messages[0]\n\n    app.messages = messages\n    app.messages_by_id = {msg.msg_id: msg for msg in messages}\n\n    db_load_message_flags(app.db, app.messages_by_id)\n    db_load_read_comments(app.db, app.messages_by_id)\n\n    app_select_message(app, selected_message, show_pager)\n\n\ndef app_load_group(app: AppState, group: Group) -> None:\n    fn = partial(group.fetch, app.db, group.page)\n    flash = f\"Fetching stories from '{group.label}' (page {group.page})...\"\n\n    if (messages := app_safe_run(app, fn, flash=flash)) is None:\n        return\n\n    app_load_messages(app, messages)\n    app.group = group\n\n\ndef app_close_thread(app: AppState) -> None:\n    selected_thread_id = app.selected_message.thread_id if app.selected_message else None\n    filtered_messages = [msg_unload(msg) for msg in app.messages if msg.is_thread]\n\n    app_load_messages(app, filtered_messages, selected_message_id=selected_thread_id)\n\n\ndef app_open_thread(app: AppState, thread_message: Message) -> None:\n    fn = partial(group_fetch_thread, thread_message.thread_id)\n    flash = f\"Fetching thread '{thread_message.thread_id}'...\"\n\n    if (new_thread_message := app_safe_run(app, fn, flash=flash)) is None:\n        return\n\n    app_close_thread(app)\n\n    index_pos = thread_message.index_position\n    thread_messages = list(msg_flatten_thread(new_thread_message, ascii=app.ascii))\n    new_thread_message.total_comments = len(thread_messages)\n    messages = app.messages[:index_pos] + thread_messages + app.messages[index_pos + 1 :]  # noqa: E203\n\n    app_load_messages(app, messages, selected_message_id=thread_message.msg_id, show_pager=True)\n\n\ndef app_update_layout(app: AppState) -> None:\n    lt = app.layout\n\n    (lt.lines, lt.cols) = app.screen.getmaxyx()\n\n    if lt.lines < 12 or lt.cols < 80:\n        raise ExitException(1, \"At least 80x12 terminal is required\")\n\n    max_index_height = lt.lines - 3\n    lt.index_height = (max_index_height // 3) if app.pager_visible else max_index_height\n\n    lt.middle_menu_row = lt.index_start + lt.index_height if app.pager_visible else None\n    lt.pager_start = lt.index_start + lt.index_height + 1 if app.pager_visible else None\n    lt.pager_height = lt.lines - lt.pager_start - 2 if lt.pager_start is not None else None\n\n    lt.bottom_menu_row = lt.lines - 2\n    lt.flash_menu_row = lt.lines - 1\n\n\ndef app_show_help_screen(app: AppState) -> None:\n    max_lines = app.layout.lines - 4\n\n    help_lines = HELP_SCREEN.split(\"\\n\")\n    help_pages = [\"\\n\".join(lines) for lines in list_chunk(help_lines, max_lines)]\n\n    for page in help_pages:\n        app.screen.erase()\n        app.screen.addstr(0, 0, \"Available commands:\\n\\n\")\n        app.screen.addstr(page)\n        app.screen.addstr(\"\\n\\nPress any key to continue...\")\n        app.screen.refresh()\n        app.screen.getch()\n\n\ndef app_show_links_screen(app: AppState) -> None:\n    lines = app.selected_message.lines if app.selected_message is not None else []\n\n    # Max amount of keys is 21 to fit on 25-line terminals\n    keys = \"1234567890abcdefghijk\"\n    urls = URL_REX.findall(\" \".join(lines))\n    items = dict(zip((ord(k) for k in keys), urls))\n\n    if len(items) == 0:\n        return app_show_flash(app, \"No links available for opening\")\n\n    app.screen.erase()\n    app.screen.addstr(0, 0, \"Select link to open:\")\n\n    for i, (key, url) in enumerate(items.items()):\n        app.screen.addstr(i + 2, 0, f\"{chr(key)} - {url}\")\n\n    app.screen.addstr(i + 4, 0, \"To change browser run: BROWSER='firefox %s' ./retronews.py\")\n    app.screen.refresh()\n\n    key = app.screen.getch()\n\n    if key not in items.keys():\n        return app_show_flash(app, \"Unknown key\")\n\n    url = items[key]\n\n    app_show_flash(app, f\"Opening {url}\")\n    webbrowser.open(url)\n\n    # Refresh window in case a terminal browser was used\n    app.screen.clearok(True)\n\n\ndef app_show_flash(app: AppState, flash: Optional[str]) -> None:\n    app.flash = flash\n    app_render(app)\n\n\ndef app_prompt(app: AppState, prompt: str) -> str:\n    lt = app.layout\n\n    app.screen.insstr(lt.flash_menu_row, 0, prompt.ljust(lt.cols))\n    app.screen.refresh()\n\n    curses.curs_set(1)\n    win = curses.newwin(1, lt.cols - len(prompt), lt.flash_menu_row, len(prompt))\n\n    textbox = curses.textpad.Textbox(win)\n    textbox.stripspaces = True\n    ret = textbox.edit().strip()\n\n    del win\n    curses.curs_set(0)\n\n    return ret\n\n\ndef app_render_index_row(app: AppState, row: int, message: Message) -> None:\n    cols = app.layout.cols\n    date = message.date.strftime(\"%Y-%m-%d %H:%M\")\n    author = (message.author or \"<unknown>\")[:10].ljust(10)\n\n    is_response = message.title.startswith(\"Re:\") and not message.is_thread\n    is_selected = message == app.selected_message\n    hide_title = is_response and row > app.layout.index_start and not message.flags.starred and not is_selected\n    title = \"\" if hide_title else text_clean(message.title, ascii=app.ascii)\n\n    unread = (\n        str(max(min(message.total_comments - message.read_comments, 9999), 0)).rjust(4) if message.is_thread else \"    \"\n    )\n\n    app.screen.insstr(row, 0, f\"[{date}]  [{author}]  [{unread}]  {message.index_tree}{title}\")\n\n    if is_selected:\n        cursor_attr = curses.A_REVERSE if app.monochrome else 0\n        app.screen.chgat(row, 0, cols, app.colors[\"cursor\"] | cursor_attr)\n    else:\n        read_attr = 0 if message.is_shown_as_read else curses.A_BOLD\n        subject_attr = app.colors[\"starred_subject\"] if message.flags.starred else app.colors[\"default\"]\n        subject_attr = subject_attr | read_attr\n\n        app.screen.chgat(row, 1, 16, app.colors[\"date\"] | read_attr)\n        app.screen.chgat(row, 21, 10, app.colors[\"author\"] | read_attr)\n        app.screen.chgat(row, 35, 4, app.colors[\"unread_comments\"] | read_attr)\n        app.screen.chgat(row, 42, len(message.index_tree), app.colors[\"tree\"])\n        app.screen.chgat(row, 42 + len(message.index_tree), cols - 42 - len(message.index_tree), subject_attr)\n\n\ndef app_render_index(app: AppState) -> None:\n    height = app.layout.index_height\n\n    offset = app.selected_message.index_position - height // 2 if app.selected_message else 0\n    offset = min(offset, len(app.messages) - height)\n    offset = max(offset, 0)\n\n    rows_to_render = min(height, len(app.messages) - offset)\n\n    for i in range(rows_to_render):\n        app_render_index_row(app, app.layout.index_start + i, app.messages[i + offset])\n\n\ndef app_get_pager_line_attr(app: AppState, line: str) -> int:\n    if line.startswith(\"Content-Location: \"):\n        return app.colors[\"tree\"]\n    elif line.startswith(\"Date: \"):\n        return app.colors[\"date\"]\n    elif line.startswith(\"From: \"):\n        return app.colors[\"author\"]\n    elif line.startswith(\"Subject: \"):\n        return app.colors[\"header_subject\"]\n    elif line.startswith(\">>\") or line.startswith(\"> >\"):\n        return app.colors[\"nested_quote\"]\n    elif line.startswith(\">\"):\n        return app.colors[\"quote\"]\n    elif line.startswith(\"| \"):\n        return app.colors[\"code\"]\n    elif line == \"~\":\n        return app.colors[\"empty_pager_line\"]\n    elif line == \"<deleted>\" or line == \"[dead]\":\n        return app.colors[\"deleted_message_pager_line\"]\n    else:\n        return 0\n\n\ndef app_render_pager_line(app: AppState, row: int, line: str) -> None:\n    hl_lines = not app.raw_mode\n    line_attr = app_get_pager_line_attr(app, line) if hl_lines else 0\n    hl_urls = line_attr == 0 and hl_lines\n\n    line = text_clean(line, ascii=app.ascii)\n\n    app.screen.move(row, 0)\n    app.screen.clrtoeol()\n    app.screen.move(row, 0)\n\n    for part in text_split_urls(line):\n        is_url = URL_REX.fullmatch(part)\n        part_attr = app.colors[\"url\"] if is_url and hl_urls else line_attr\n        app.screen.addstr(part, part_attr)\n\n\ndef app_render_pager(app: AppState) -> None:\n    message = app.selected_message\n    start = app.layout.pager_start\n    height = app.layout.pager_height\n\n    if message is None or start is None or height is None:\n        return\n\n    for i in range(height):\n        line = list_get(message.lines, i + app.pager_offset, \"~\")\n        app_render_pager_line(app, i + start, line)\n\n\ndef app_render_top_menu(app: AppState) -> None:\n    lt = app.layout\n    cols = lt.cols\n    base_attr = curses.A_REVERSE if app.monochrome else curses.A_BOLD\n    app.screen.insstr(lt.top_menu_row, 0, HELP_MENU[:cols].ljust(cols), app.colors[\"menu\"] | base_attr)\n\n\ndef app_render_middle_menu(app: AppState) -> None:\n    if (row := app.layout.middle_menu_row) is None:\n        return\n\n    if (message := app.selected_message) is None:\n        return\n\n    if (thread_message := app.messages_by_id.get(message.thread_id)) is None:\n        return\n\n    cols = app.layout.cols\n    total = thread_message.total_comments\n    unread = total - thread_message.read_comments\n\n    text = f\"--({unread}/{total} unread)\"\n    if thread_message.flags.starred:\n        text += \"--(starred thread)\"\n    if app.raw_mode:\n        text += \"--(raw mode on)\"\n    text = text[:cols].ljust(cols, \"-\")\n\n    base_attr = curses.A_REVERSE if app.monochrome else curses.A_BOLD\n    app.screen.insstr(row, 0, text, app.colors[\"menu\"] | base_attr)\n\n\ndef app_render_bottom_menu(app: AppState) -> None:\n    lt = app.layout\n    base_attr = curses.A_REVERSE if app.monochrome else curses.A_BOLD\n\n    app.screen.chgat(lt.bottom_menu_row, 0, lt.cols, app.colors[\"menu\"] | base_attr)\n    app.screen.move(lt.bottom_menu_row, 0)\n\n    for i, group in enumerate(GROUP_TABS):\n        is_active = group.label == app.group.label\n        item_attr = app.colors[\"menu_active\"] | curses.A_BOLD if is_active else app.colors[\"menu\"]\n        item_attr = item_attr | base_attr\n        app.screen.addstr(f\"{i+1}:{group.label}\", item_attr)\n        app.screen.addstr(\"  \", app.colors[\"menu\"] | base_attr)\n\n    page_text = f\"page: {app.group.page}\"\n    app.screen.insstr(lt.bottom_menu_row, lt.cols - len(page_text), page_text, app.colors[\"menu\"] | base_attr)\n\n\ndef app_render(app: AppState) -> None:\n    app_update_layout(app)\n    app.screen.erase()\n    app_render_index(app)\n    app_render_pager(app)\n    app_render_top_menu(app)\n    app_render_middle_menu(app)\n    app_render_bottom_menu(app)\n    app.screen.insstr(app.layout.flash_menu_row, 0, app.flash or \"\")\n    app.screen.refresh()\n\n\ndef app_init_colors(app: AppState) -> None:\n    if app.monochrome:\n        app.colors = defaultdict(lambda: 0)\n        return\n\n    try:\n        curses.use_default_colors()\n        for i, (name, (fg, bg)) in enumerate(COLORS.items()):\n            curses.init_pair(i + 1, fg, bg)\n            app.colors[name] = curses.color_pair(i + 1)\n    except curses.error:\n        app.colors = defaultdict(lambda: 0)\n        app.monochrome = True\n\n\ndef app_main(screen: Window, db: DB, group: Group, ascii: bool, monochrome: bool) -> int:\n    curses.curs_set(0)\n\n    app = AppState(screen=screen, db=db, group=group, ascii=ascii, monochrome=monochrome)\n\n    app_init_colors(app)\n    app_load_group(app, app.group)\n\n    while True:\n        app_render(app)\n        app.flash = \"\"\n        c = app.screen.getch()\n        KEY_BINDINGS.get(c, cmd_unknown)(app)\n\n\ndef setup_logging(path: Optional[str]) -> None:\n    if path is None:\n        return logging.disable()\n\n    format = \"%(asctime)s %(levelname)s: %(message)s\"\n    stream = sys.stderr if path == \"-\" else open(path, \"a\")\n    logging.basicConfig(format=format, level=\"DEBUG\", stream=stream)\n    logging.debug(\"Session started\")\n\n\ndef run_rcfile(path: str) -> None:\n    path = os.path.expanduser(path)\n\n    if not os.path.isfile(path):\n        return\n\n    code = compile(open(path).read(), path, \"exec\")\n    exec(code, {\"retronews\": sys.modules[__name__]})\n\n\nif __name__ == \"__main__\":\n    tab_choices = range(1, len(GROUP_TABS) + 1)\n\n    ap = argparse.ArgumentParser(\n        formatter_class=lambda prog: argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=32)\n    )\n    ap.add_argument(\"--ascii\", action=\"store_true\", help=\"show only ascii characters\")\n    ap.add_argument(\"--monochrome\", action=\"store_true\", help=\"disable colors\")\n    ap.add_argument(\"-c\", \"--rcfile\", metavar=\"PATH\", default=\"~/.retronewsrc.py\", help=\"optional startup code path\")\n    ap.add_argument(\"-d\", \"--db\", metavar=\"PATH\", default=\"~/.retronews.db\", help=\"database path\")\n    ap.add_argument(\"-l\", \"--logfile\", metavar=\"PATH\", default=None, help=\"debug logfile path\")\n    ap.add_argument(\"-t\", \"--tab\", metavar=\"TAB\", type=int, default=1, choices=tab_choices, help=\"initial tab\")\n    ap.add_argument(\"-r\", \"--render\", metavar=\"PATH\", default=None, help=\"render raw html message and quit\")\n    ap.add_argument(\"-m\", \"--msg\", metavar=\"URL\", default=None, help=\"render message from URL\")\n    args = ap.parse_args()\n\n    setup_logging(args.logfile)\n    run_rcfile(args.rcfile)\n\n    if (path := args.render) is not None:\n        with open(path) as fp:\n            print(html_render(fp.read()))\n        sys.exit(0)\n\n    try:\n        db = db_init(args.db)\n\n        if args.msg:\n            group = group_for_msg_url(args.msg)\n        else:\n            group = GROUP_TABS[args.tab - 1]\n\n        ascii = args.ascii\n        monochrome = args.monochrome or \"NO_COLOR\" in os.environ\n\n        ret = curses.wrapper(app_main, db=db, group=group, ascii=ascii, monochrome=monochrome)\n    except ExitException as e:\n        if e.message:\n            sys.stderr.write(e.message + \"\\n\")\n        ret = e.code\n    except BaseException as e:\n        sys.stderr.write(\"\\n\".join(traceback.format_exception(e)))\n        ret = 1\n    finally:\n        db.close()\n        sys.exit(ret)\n"
  },
  {
    "path": "tests/test_hn_01.html",
    "content": "<p>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p><p>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p>"
  },
  {
    "path": "tests/test_hn_01.out",
    "content": "Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\nte. Noster nominati recteque no has.\n\nLorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\nte. Noster nominati recteque no has."
  },
  {
    "path": "tests/test_hn_02.html",
    "content": "<p>&gt;&gt;Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p><p>&gt; Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p><p>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p>\n"
  },
  {
    "path": "tests/test_hn_02.out",
    "content": ">>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\n>>te. Noster nominati recteque no has.\n\n> Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\n> te. Noster nominati recteque no has.\n\nLorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\nte. Noster nominati recteque no has."
  },
  {
    "path": "tests/test_hn_03.html",
    "content": "<a href=\"https://example.com/foo/bar\">https://example.com/foo...</a>\n"
  },
  {
    "path": "tests/test_hn_03.out",
    "content": "https://example.com/foo/bar"
  },
  {
    "path": "tests/test_hn_04.html",
    "content": "<p>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus</p><p>[0]: <a>https://long.long.long.long.long.long.long.long.long.long.long.example.com</a></p><p>[1] - <a>https://long.long.long.long.long.long.long.long.long.long.long.example.com</a></p><p>[2] <a>https://long.long.long.long.long.long.long.long.long.long.long.example.com</a></p>\n"
  },
  {
    "path": "tests/test_hn_04.out",
    "content": "Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\n\n[0]: https://long.long.long.long.long.long.long.long.long.long.long.example.com\n\n[1] - https://long.long.long.long.long.long.long.long.long.long.long.example.com\n\n[2] https://long.long.long.long.long.long.long.long.long.long.long.example.com"
  },
  {
    "path": "tests/test_hn_05.html",
    "content": "<p>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p><p><pre><code>    def hello():\n        print('Hello World')\n        return None\n</code></pre>\nLorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p><p><pre><code>    def hello():\n        print('Hello World')\n        return None\n</code></pre>\nLorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus te. Noster nominati recteque no has.</p>\n"
  },
  {
    "path": "tests/test_hn_05.out",
    "content": "Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\nte. Noster nominati recteque no has.\n\n| def hello():\n|     print('Hello World')\n|     return None\n\nLorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\nte. Noster nominati recteque no has.\n\n| def hello():\n|     print('Hello World')\n|     return None\n\nLorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus\nte. Noster nominati recteque no has."
  },
  {
    "path": "tests/test_hn_06.html",
    "content": "<p>Lorem ipsum dolor sit amet, pro eu soleat civibus.</p><p><pre><code>    lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])\n</code></pre>Mel quas sensibus te. Noster nominati recteque no has.</p>\n"
  },
  {
    "path": "tests/test_hn_06.out",
    "content": "Lorem ipsum dolor sit amet, pro eu soleat civibus.\n\n| lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])\n\nMel quas sensibus te. Noster nominati recteque no has."
  },
  {
    "path": "tests/test_lb_01.html",
    "content": "<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n"
  },
  {
    "path": "tests/test_lb_01.out",
    "content": "> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n> eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\n> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n> eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor"
  },
  {
    "path": "tests/test_lb_02.html",
    "content": "<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem <code>ipsum</code> dolor sit amet, <code>consectetur adipiscing</code> elit, sed do eiusmod tempor</p>\n<pre><code>def hello_world():\n  print('hello')\n  if True:\n    print('world')\n  print('!!!')\n\nhello_world()\n</code></pre>\n"
  },
  {
    "path": "tests/test_lb_02.out",
    "content": "> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n> eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n> elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n> adipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem `ipsum` dolor sit amet,\n`consectetur adipiscing` elit, sed do eiusmod tempor\n\n| def hello_world():\n|   print('hello')\n|   if True:\n|     print('world')\n|   print('!!!')\n| \n| hello_world()"
  },
  {
    "path": "tests/test_lb_03.html",
    "content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<ul>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</li>\n<li>Lorem:\n<ul>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</li>\n<li>Lorem ipsum dolor sit amet, consectetur.</li>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</li>\n</ul>\n</li>\n</ul>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n"
  },
  {
    "path": "tests/test_lb_03.out",
    "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\n- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n  eiusmod tempor.\n- Lorem:\n\n  - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n    eiusmod tempor.\n  - Lorem ipsum dolor sit amet, consectetur.\n  - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n    eiusmod tempor.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor"
  },
  {
    "path": "tests/test_lb_04.html",
    "content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, <a href=\"https://example.com/\" rel=\"ugc\">example</a> sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur <code>adipiscing elit</code>, sed do eiusmod tempor</p>\n<blockquote>\n<p>\n  Lorem ipsum <em>dolor</em> <b>sit<br>dolor</b> amet, consectetur\n\n   adipiscing  \n  \n  \n    elit\n    </p>\n<hr>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>"
  },
  {
    "path": "tests/test_lb_04.out",
    "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\n> Lorem ipsum dolor sit amet, consectetur adipiscing elit, example\n> https://example.com/ sed do eiusmod tempor. Lorem ipsum dolor sit\n> amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem\n> ipsum dolor sit amet, consectetur `adipiscing elit`, sed do eiusmod\n> tempor\n> \n>> Lorem ipsum /dolor/ *sit\n>> dolor* amet, consectetur adipiscing elit\n>> \n>> ------------------------------------------------------------------\n>> \n>> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n>> eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n>> elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet,\n>> consectetur adipiscing elit, sed do eiusmod tempor\n> \n> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n> eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n> elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n> adipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit"
  },
  {
    "path": "tests/test_lb_05.html",
    "content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<hr>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<hr>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n"
  },
  {
    "path": "tests/test_lb_05.out",
    "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\n----------------------------------------------------------------------\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\n----------------------------------------------------------------------\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor"
  },
  {
    "path": "tests/test_lb_06.html",
    "content": "<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<blockquote>\n<ul>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>\n</ul>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n"
  },
  {
    "path": "tests/test_lb_06.out",
    "content": "> Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\n\n> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n> eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n> elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n> adipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit\n\n> - Lorem ipsum dolor sit amet, consectetur adipiscing elit\n> - Lorem ipsum dolor sit amet, consectetur adipiscing elit\n> - Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor"
  },
  {
    "path": "tests/test_lb_07.html",
    "content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<ol>\n<li>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</li>\n<li>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</li>\n<li>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</li>\n</ol>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n"
  },
  {
    "path": "tests/test_lb_07.out",
    "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\n- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n  eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n  elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n  adipiscing elit, sed do eiusmod tempor\n- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n  eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n  elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n  adipiscing elit, sed do eiusmod tempor\n- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n  eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n  elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n  adipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\neiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor"
  },
  {
    "path": "tests/test_lb_08.html",
    "content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<blockquote>\n<blockquote>\n<ol start=\"3\">\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</li>\n<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>\n</ol>\n</blockquote>\n</blockquote>\n<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n"
  },
  {
    "path": "tests/test_lb_08.out",
    "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\n>> - Lorem ipsum dolor sit amet, consectetur adipiscing elit\n>> - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n>>   eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n>>   adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor sit\n>>   amet, consectetur adipiscing elit, sed do eiusmod tempor\n>> - Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\n> Lorem ipsum dolor sit amet, consectetur adipiscing elit\n> \n> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n> eiusmod tempor. Lorem ipsum dolor sit amet, consectetur adipiscing\n> elit, sed do eiusmod tempor. Lorem ipsum dolor sit amet, consectetur\n> adipiscing elit, sed do eiusmod tempor\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit"
  },
  {
    "path": "tests/test_lb_09.html",
    "content": "<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n</blockquote>\n<blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<pre><code>def hello_world():\n  print('hello')\n  if True:\n    print('world')\n  print('!!!')\n\nhello_world()\n</code></pre>\n</blockquote>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n<pre><code>def hello_world():\n  print('hello')\n  if True:\n    print('world')\n  print('!!!')\n\nhello_world()\n</code></pre>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>\n"
  },
  {
    "path": "tests/test_lb_09.out",
    "content": "> Lorem ipsum dolor sit amet, consectetur adipiscing elit\n\n> Lorem ipsum dolor sit amet, consectetur adipiscing elit\n> \n> | def hello_world():\n> |   print('hello')\n> |   if True:\n> |     print('world')\n> |   print('!!!')\n> | \n> | hello_world()\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit\n\n| def hello_world():\n|   print('hello')\n|   if True:\n|     print('world')\n|   print('!!!')\n| \n| hello_world()\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit"
  },
  {
    "path": "tests.py",
    "content": "import os\nimport subprocess\nimport unittest\n\nimport retronews\n\nTC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), \"tests\")\n\n\nclass TestHtmlRender(unittest.TestCase):\n    maxDiff = None\n\n    def checkRendering(self, name: str):\n        html_path = os.path.join(TC_DIR, f\"{name}.html\")\n        out_path = os.path.join(TC_DIR, f\"{name}.out\")\n\n        with open(html_path) as fp:\n            html = fp.read()\n\n        actual = retronews.html_render(html)\n\n        if not os.path.exists(out_path):\n            with open(out_path, \"w\") as fp:\n                fp.write(actual)\n            return\n\n        cmd = [\"diff\", \"-Nru\", \"--color=always\", out_path, \"-\"]\n        proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)\n        stdout, stderr = proc.communicate(input=actual)\n\n        if proc.returncode != 0:\n            self.fail(f\"Unexpected rendering output\\n{stdout}\")\n\n\ndef setup_test_cases():\n    tcs = [x.split(\".\")[0] for x in sorted(os.listdir(TC_DIR)) if x.endswith(\".html\")]\n\n    for tc in tcs:\n        setattr(TestHtmlRender, tc, lambda self, tc=tc: self.checkRendering(tc))\n\n\nif __name__ == \"__main__\":\n    setup_test_cases()\n    unittest.main()\n"
  }
]