|
50 | 50 | "from fastcore.xml import *\n",
|
51 | 51 | "from fastcore.script import *\n",
|
52 | 52 | "import httpx\n",
|
53 |
| - "from urllib.parse import urlparse" |
| 53 | + "from urllib.parse import urlparse\n", |
| 54 | + "from nbdev.config import get_config" |
54 | 55 | ]
|
55 | 56 | },
|
56 | 57 | {
|
|
185 | 186 | {
|
186 | 187 | "data": {
|
187 | 188 | "text/plain": [
|
188 |
| - "{'title': 'FastHTML quick start'}" |
| 189 | + "{'title': 'internal docs - ed'}" |
189 | 190 | ]
|
190 | 191 | },
|
191 | 192 | "execution_count": null,
|
|
217 | 218 | {
|
218 | 219 | "data": {
|
219 | 220 | "text/plain": [
|
220 |
| - "{'title': 'FastHTML quick start',\n", |
221 |
| - " 'url': 'https://docs.fastht.ml/tutorials/quickstart_for_web_devs.html.md'}" |
| 221 | + "{'title': 'internal docs - ed', 'url': 'https://llmstxt.org/ed.html'}" |
222 | 222 | ]
|
223 | 223 | },
|
224 | 224 | "execution_count": null,
|
|
674 | 674 | "from fastcore.xml import Sections,Project,Doc"
|
675 | 675 | ]
|
676 | 676 | },
|
677 |
| - { |
678 |
| - "cell_type": "code", |
679 |
| - "execution_count": null, |
680 |
| - "metadata": {}, |
681 |
| - "outputs": [], |
682 |
| - "source": [ |
683 |
| - "#| export\n", |
684 |
| - "def find_root_dir():\n", |
685 |
| - " \"Find the root directory of the nbdev project by looking for settings.ini\"\n", |
686 |
| - " path = Path.cwd()\n", |
687 |
| - " while path != path.parent:\n", |
688 |
| - " if (path / 'settings.ini').exists(): return path\n", |
689 |
| - " path = path.parent\n", |
690 |
| - " return None" |
691 |
| - ] |
692 |
| - }, |
693 | 677 | {
|
694 | 678 | "cell_type": "code",
|
695 | 679 | "execution_count": null,
|
696 | 680 | "metadata": {},
|
697 | 681 | "outputs": [],
|
698 | 682 | "source": [
|
699 | 683 | "#|export\n",
|
| 684 | + "def _local_docs_pth(cfg): return cfg.config_path/'_proc'/cfg.doc_path.name\n", |
| 685 | + "\n", |
700 | 686 | "def get_doc_content(url):\n",
|
701 | 687 | " \"Fetch content from local file if in nbdev repo.\"\n",
|
702 |
| - " root_dir = find_root_dir()\n", |
703 |
| - " if root_dir:\n", |
704 |
| - " config = Config(root_dir, 'settings.ini')\n", |
705 |
| - " doc_host = config.get('doc_host')\n", |
706 |
| - " if doc_host and url.startswith(doc_host):\n", |
707 |
| - " parsed_url = urlparse(url)\n", |
708 |
| - " relative_path = parsed_url.path.lstrip('/')\n", |
709 |
| - " local_path = root_dir / '_docs' / relative_path\n", |
710 |
| - " if local_path.exists():\n", |
711 |
| - " with open(local_path, 'r') as f: return f.read()\n", |
712 |
| - " # If not a local file or file doesn't exist, fetch from URL\n", |
| 688 | + " cfg = get_config()\n", |
| 689 | + " if url.startswith(cfg.doc_host):\n", |
| 690 | + " relative_path = urlparse(url).path.lstrip('/')\n", |
| 691 | + " local_path = _local_docs_pth(cfg) / relative_path\n", |
| 692 | + " if local_path.exists(): return local_path.read_text()\n", |
713 | 693 | " return httpx.get(url).text"
|
714 | 694 | ]
|
715 | 695 | },
|
|
797 | 777 | {
|
798 | 778 | "data": {
|
799 | 779 | "text/plain": [
|
800 |
| - "{'docs': {'FastHTML quick start': 27376,\n", |
| 780 | + "{'docs': {'internal docs - ed': 34464,\n", |
| 781 | + " 'FastHTML quick start': 27376,\n", |
801 | 782 | " 'HTMX reference': 26427,\n",
|
802 | 783 | " 'Starlette quick guide': 7936},\n",
|
803 | 784 | " 'examples': {'Todo list application': 18558},\n",
|
|
821 | 802 | {
|
822 | 803 | "data": {
|
823 | 804 | "text/plain": [
|
824 |
| - "129814" |
| 805 | + "164321" |
825 | 806 | ]
|
826 | 807 | },
|
827 | 808 | "execution_count": null,
|
|
858 | 839 | "def llms_txt2ctx(\n",
|
859 | 840 | " fname:str, # File name to read\n",
|
860 | 841 | " optional:bool_arg=False, # Include 'optional' section?\n",
|
861 |
| - " n_workers:int=None # Number of threads to use for parallel downloading\n", |
| 842 | + " n_workers:int=None, # Number of threads to use for parallel downloading\n", |
| 843 | + " save_nbdev_fname:str=None #save output to nbdev `{docs_path}` instead of emitting to stdout\n", |
862 | 844 | "):\n",
|
863 | 845 | " \"Print a `Project` with a `Section` for each H2 part in file read from `fname`, optionally skipping the 'optional' section.\"\n",
|
864 |
| - " print(create_ctx(Path(fname).read_text(), optional=optional, n_workers=n_workers))" |
| 846 | + " ctx = create_ctx(Path(fname).read_text(), optional=optional, n_workers=n_workers)\n", |
| 847 | + " if save_nbdev_fname:\n", |
| 848 | + " cfg = get_config()\n", |
| 849 | + " (_local_docs_pth(cfg) / save_nbdev_fname).mk_write(ctx)\n", |
| 850 | + " else: print(ctx)" |
| 851 | + ] |
| 852 | + }, |
| 853 | + { |
| 854 | + "cell_type": "code", |
| 855 | + "execution_count": null, |
| 856 | + "metadata": {}, |
| 857 | + "outputs": [], |
| 858 | + "source": [ |
| 859 | + "Path('/Users/hamel/github/fastcore/_docs/llms-ctx-full.txt').mk_write('hello')" |
865 | 860 | ]
|
866 | 861 | },
|
867 | 862 | {
|
|
0 commit comments