Skip to content

Commit

Permalink
modify dir
Browse files Browse the repository at this point in the history
  • Loading branch information
hamelsmu committed Sep 13, 2024
1 parent ef3718b commit dfb74ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions llms_txt/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'git_url': 'https://github.com/AnswerDotAI/llms-txt',
'lib_path': 'llms_txt'},
'syms': { 'llms_txt.core': { 'llms_txt.core._doc': ('core.html#_doc', 'llms_txt/core.py'),
'llms_txt.core._local_docs_pth': ('core.html#_local_docs_pth', 'llms_txt/core.py'),
'llms_txt.core._parse_links': ('core.html#_parse_links', 'llms_txt/core.py'),
'llms_txt.core._parse_llms': ('core.html#_parse_llms', 'llms_txt/core.py'),
'llms_txt.core._section': ('core.html#_section', 'llms_txt/core.py'),
Expand Down
10 changes: 7 additions & 3 deletions llms_txt/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ def parse_llms_file(txt):
from fastcore.xml import Sections,Project,Doc

# %% ../nbs/01_core.ipynb
def _local_docs_pth(cfg): return cfg.config_path/'_proc'/cfg.doc_path.name

def get_doc_content(url):
"Fetch content from local file if in nbdev repo."
cfg = get_config()
if url.startswith(cfg.doc_host):
relative_path = urlparse(url).path.lstrip('/')
local_path = cfg.doc_path / relative_path
local_path = _local_docs_pth(cfg) / relative_path
if local_path.exists(): return local_path.read_text()
return httpx.get(url).text

Expand Down Expand Up @@ -118,9 +120,11 @@ def llms_txt2ctx(
fname:str, # File name to read
optional:bool_arg=False, # Include 'optional' section?
n_workers:int=None, # Number of threads to use for parallel downloading
save_nbdev:bool_arg=False #save output to nbdev `docs_path` instead of emitting to stdout
save_nbdev_fname:str=None #save output to nbdev `{docs_path}` instead of emitting to stdout
):
"Print a `Project` with a `Section` for each H2 part in file read from `fname`, optionally skipping the 'optional' section."
ctx = create_ctx(Path(fname).read_text(), optional=optional, n_workers=n_workers)
if save_nbdev: (get_config().doc_path / fname).write_text(ctx)
if save_nbdev_fname:
cfg = get_config()
(_local_docs_pth(cfg) / save_nbdev_fname).mk_write(ctx)
else: print(ctx)
29 changes: 21 additions & 8 deletions nbs/01_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
{
"data": {
"text/plain": [
"{'title': 'FastHTML quick start'}"
"{'title': 'internal docs - ed'}"
]
},
"execution_count": null,
Expand Down Expand Up @@ -218,8 +218,7 @@
{
"data": {
"text/plain": [
"{'title': 'FastHTML quick start',\n",
" 'url': 'https://docs.fastht.ml/tutorials/quickstart_for_web_devs.html.md'}"
"{'title': 'internal docs - ed', 'url': 'https://llmstxt.org/ed.html'}"
]
},
"execution_count": null,
Expand Down Expand Up @@ -682,12 +681,14 @@
"outputs": [],
"source": [
"#|export\n",
"def _local_docs_pth(cfg): return cfg.config_path/'_proc'/cfg.doc_path.name\n",
"\n",
"def get_doc_content(url):\n",
" \"Fetch content from local file if in nbdev repo.\"\n",
" cfg = get_config()\n",
" if url.startswith(cfg.doc_host):\n",
" relative_path = urlparse(url).path.lstrip('/')\n",
" local_path = cfg.doc_path / relative_path\n",
" local_path = _local_docs_pth(cfg) / relative_path\n",
" if local_path.exists(): return local_path.read_text()\n",
" return httpx.get(url).text"
]
Expand Down Expand Up @@ -776,7 +777,8 @@
{
"data": {
"text/plain": [
"{'docs': {'FastHTML quick start': 27376,\n",
"{'docs': {'internal docs - ed': 34464,\n",
" 'FastHTML quick start': 27376,\n",
" 'HTMX reference': 26427,\n",
" 'Starlette quick guide': 7936},\n",
" 'examples': {'Todo list application': 18558},\n",
Expand All @@ -800,7 +802,7 @@
{
"data": {
"text/plain": [
"129814"
"164321"
]
},
"execution_count": null,
Expand Down Expand Up @@ -838,14 +840,25 @@
" fname:str, # File name to read\n",
" optional:bool_arg=False, # Include 'optional' section?\n",
" n_workers:int=None, # Number of threads to use for parallel downloading\n",
" save_nbdev:bool_arg=False #save output to nbdev `docs_path` instead of emitting to stdout\n",
" save_nbdev_fname:str=None #save output to nbdev `{docs_path}` instead of emitting to stdout\n",
"):\n",
" \"Print a `Project` with a `Section` for each H2 part in file read from `fname`, optionally skipping the 'optional' section.\"\n",
" ctx = create_ctx(Path(fname).read_text(), optional=optional, n_workers=n_workers)\n",
" if save_nbdev: (get_config().doc_path / fname).write_text(ctx)\n",
" if save_nbdev_fname:\n",
" cfg = get_config()\n",
" (_local_docs_pth(cfg) / save_nbdev_fname).mk_write(ctx)\n",
" else: print(ctx)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Path('/Users/hamel/github/fastcore/_docs/llms-ctx-full.txt').mk_write('hello')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit dfb74ef

Please sign in to comment.