diff --git a/nbdev_mkdocs/_cli.py b/nbdev_mkdocs/_cli.py index 2b7c883..e460412 100644 --- a/nbdev_mkdocs/_cli.py +++ b/nbdev_mkdocs/_cli.py @@ -100,11 +100,11 @@ def generate( ), prompt: str = typer.Option( "Cute animal wearing hoodie sitting in high chair in purple room, browsing computer, 3d render", - help="The prompt to use for generating the image.", + help="The prompt to use for generating the image. Please pass this option only when using 'dall_e' as the generator.", ), image_path: Optional[str] = typer.Option( None, - help="Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used.", + help="Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used. Please pass this option only when using 'file' as the generator.", ), ) -> None: """CLI command for generating a custom social share image""" diff --git a/nbdev_mkdocs/social_image_generator.py b/nbdev_mkdocs/social_image_generator.py index 7dcc34c..62a9840 100644 --- a/nbdev_mkdocs/social_image_generator.py +++ b/nbdev_mkdocs/social_image_generator.py @@ -196,7 +196,9 @@ def _update_social_image_in_mkdocs_yml(root_path: str, image_url: str) -> None: The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen) """ image_url = ( - "overrides/images/social_image.png" if is_local_path(image_url) else image_url + image_url + if "https://opengraph.githubassets.com" in image_url + else "overrides/images/social_image.png" ) mkdocs_yml_path = Path(root_path) / "mkdocs" / "mkdocs.yml" @@ -205,23 +207,17 @@ def _update_social_image_in_mkdocs_yml(root_path: str, image_url: str) -> None: yaml.dump(config, mkdocs_yml_path, transform=_unescape_exclamation_mark) # %% ../nbs/Social_Image_Generator.ipynb 19 -def _update_social_image_in_site_overrides(root_path: str, image_url: str) -> None: +def _update_social_image_in_site_overrides(root_path: str) -> None: """Update social image link in site_overrides HTML template Args: root_path: The root path of the project - image_url: The social image url !!! note The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen) """ - _replace_str = ( - 'config.site_url ~ ("" if config.site_url.endswith("/") else "/") ~ config.extra.social_image ' - if is_local_path(image_url) - else "config.extra.social_image " - ) - + _replace_str = 'config.site_url ~ ("" if config.site_url.endswith("/") else "/") ~ config.extra.social_image ' with set_cwd(root_path): site_overrides_path = ( Path(root_path) / "mkdocs" / "site_overrides" / "main.html" @@ -240,7 +236,7 @@ def _update_social_image_in_site_overrides(root_path: str, image_url: str) -> No with open(site_overrides_path, "w", encoding="utf-8") as f: f.write(_new_text) -# %% ../nbs/Social_Image_Generator.ipynb 22 +# %% ../nbs/Social_Image_Generator.ipynb 21 class _IMG_Generator(str, Enum): """An enumeration class for the different types of image generators. @@ -313,7 +309,7 @@ def _generate_image_url( return image_url -# %% ../nbs/Social_Image_Generator.ipynb 29 +# %% ../nbs/Social_Image_Generator.ipynb 28 async def generate_social_image( root_path: str, generator: str = "file", @@ -338,4 +334,4 @@ async def generate_social_image( _update_social_image_in_mkdocs_yml(root_path, image_url) - _update_social_image_in_site_overrides(root_path, image_url) + _update_social_image_in_site_overrides(root_path) diff --git a/nbs/CLI.ipynb b/nbs/CLI.ipynb index 1194f20..38f7bc0 100644 --- a/nbs/CLI.ipynb +++ b/nbs/CLI.ipynb @@ -168,11 +168,11 @@ " ),\n", " prompt: str = typer.Option(\n", " \"Cute animal wearing hoodie sitting in high chair in purple room, browsing computer, 3d render\",\n", - " help=\"The prompt to use for generating the image.\",\n", + " help=\"The prompt to use for generating the image. Please pass this option only when using 'dall_e' as the generator.\",\n", " ),\n", " image_path: Optional[str] = typer.Option(\n", " None,\n", - " help=\"Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used.\",\n", + " help=\"Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used. Please pass this option only when using 'file' as the generator.\",\n", " ),\n", " ) -> None:\n", " \"\"\"CLI command for generating a custom social share image\"\"\"\n", diff --git a/nbs/Social_Image_Generator.ipynb b/nbs/Social_Image_Generator.ipynb index b61583d..b3a804c 100644 --- a/nbs/Social_Image_Generator.ipynb +++ b/nbs/Social_Image_Generator.ipynb @@ -404,10 +404,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp16hxplap/dst_path/mkdocs/docs_overrides/images/social_image.png'\n", - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp16hxplap/dst_path/mkdocs/docs_overrides/images/social_image.png'\n", - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp16hxplap/dst_path/mkdocs/docs_overrides/images/social_image.png'\n", - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp16hxplap/dst_path/mkdocs/docs_overrides/images/social_image.png'\n" + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp6uqfce6e/dst_path/mkdocs/docs_overrides/images/social_image.png'\n", + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp6uqfce6e/dst_path/mkdocs/docs_overrides/images/social_image.png'\n", + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp6uqfce6e/dst_path/mkdocs/docs_overrides/images/social_image.png'\n", + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp6uqfce6e/dst_path/mkdocs/docs_overrides/images/social_image.png'\n" ] } ], @@ -496,7 +496,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp4601wh7k/mkdocs/docs_overrides/images/social_image.png'\n" + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpj_4m6w6w/mkdocs/docs_overrides/images/social_image.png'\n" ] } ], @@ -752,7 +752,6 @@ "source": [ "# | export\n", "\n", - "\n", "def _update_social_image_in_mkdocs_yml(root_path: str, image_url: str) -> None:\n", " \"\"\"Update social image link in mkdocs yml file\n", "\n", @@ -765,7 +764,9 @@ " The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen)\n", " \"\"\"\n", " image_url = (\n", - " \"overrides/images/social_image.png\" if is_local_path(image_url) else image_url\n", + " image_url\n", + " if \"https://opengraph.githubassets.com\" in image_url\n", + " else \"overrides/images/social_image.png\"\n", " )\n", "\n", " mkdocs_yml_path = Path(root_path) / \"mkdocs\" / \"mkdocs.yml\"\n", @@ -784,9 +785,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpl1wm149b/mkdocs/docs_overrides/images/social_image.png'\n", - "https://my-random-domain/sample.png\n", + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpc1i3ujc1/mkdocs/docs_overrides/images/social_image.png'\n", + "overrides/images/social_image.png\n", "overrides/images/social_image.png\n", + "https://opengraph.githubassets.com/13123.123/user/repo/sample.png\n", "site_name: Test site\n", "theme:\n", " name: material\n", @@ -825,7 +827,7 @@ " emoji_index: !!python/name:materialx.emoji.twemoji\n", " emoji_generator: !!python/name:materialx.emoji.to_svg\n", "extra:\n", - " social_image: \"overrides/images/social_image.png\"\n", + " social_image: \"https://opengraph.githubassets.com/13123.123/user/repo/sample.png\"\n", "\n" ] } @@ -849,23 +851,24 @@ " mkdocs_yml_path = Path(d) / \"mkdocs\" / \"mkdocs.yml\"\n", " with mkdocs_yml_path.open(\"w\") as f:\n", " f.write(test_mkdocs_yml)\n", + " \n", + " for image_url in [\"https://my-random-domain/sample.png\", str(Path(d) / \"sample-image.png\")]:\n", + " _update_social_image_in_mkdocs_yml(d, image_url)\n", + " yaml = YAML()\n", + " config = yaml.load((Path(d) / \"mkdocs/mkdocs.yml\"))\n", + " print(config[\"extra\"][\"social_image\"])\n", + " assert (\n", + " config[\"extra\"][\"social_image\"] == \"overrides/images/social_image.png\"\n", + " ), config[\"extra\"][\"social_image\"]\n", "\n", - " image_url = \"https://my-random-domain/sample.png\"\n", - " _update_social_image_in_mkdocs_yml(d, image_url)\n", - "\n", - " yaml = YAML()\n", - " config = yaml.load((Path(d) / \"mkdocs/mkdocs.yml\"))\n", - " print(config[\"extra\"][\"social_image\"])\n", - " assert config[\"extra\"][\"social_image\"] == image_url, config[\"extra\"][\"social_image\"]\n", - "\n", - " image_url = Path(d) / \"sample-image.png\"\n", + " image_url = \"https://opengraph.githubassets.com/13123.123/user/repo/sample.png\"\n", " _update_social_image_in_mkdocs_yml(d, image_url)\n", "\n", " yaml = YAML()\n", " config = yaml.load((Path(d) / \"mkdocs/mkdocs.yml\"))\n", " print(config[\"extra\"][\"social_image\"])\n", " assert (\n", - " config[\"extra\"][\"social_image\"] == \"overrides/images/social_image.png\"\n", + " config[\"extra\"][\"social_image\"] == image_url\n", " ), config[\"extra\"][\"social_image\"]\n", "\n", " with mkdocs_yml_path.open(\"r\") as f:\n", @@ -885,23 +888,17 @@ "# | export\n", "\n", "\n", - "def _update_social_image_in_site_overrides(root_path: str, image_url: str) -> None:\n", + "def _update_social_image_in_site_overrides(root_path: str) -> None:\n", " \"\"\"Update social image link in site_overrides HTML template\n", "\n", " Args:\n", " root_path: The root path of the project\n", - " image_url: The social image url\n", "\n", " !!! note\n", "\n", " The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen)\n", " \"\"\"\n", - " _replace_str = (\n", - " 'config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image '\n", - " if is_local_path(image_url)\n", - " else \"config.extra.social_image \"\n", - " )\n", - "\n", + " _replace_str = 'config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image '\n", " with set_cwd(root_path):\n", " site_overrides_path = (\n", " Path(root_path) / \"mkdocs\" / \"site_overrides\" / \"main.html\"\n", @@ -940,7 +937,6 @@ " {% elif page and page.title and not page.is_homepage %}\n", " {% set title = title ~ \" - \" ~ page.title | striptags %}\n", " {% endif %}\n", - " {% set trailing_slash = \"\" if config.site_url.endswith(\"/\") else \"/\" %}\n", " {% set image_url = config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image %}\n", " \n", " \n", @@ -977,10 +973,8 @@ " Path(\"..\") / \"mkdocs\" / \"site_overrides\" / \"main.html\",\n", " (site_overrides_path / \"main.html\"),\n", " )\n", - "\n", - " image_url = Path(d) / \"random-image.png\"\n", - " _update_social_image_in_site_overrides(d, image_url)\n", - "\n", + " \n", + " _update_social_image_in_site_overrides(d)\n", " with open((site_overrides_path / \"main.html\"), \"r\", encoding=\"utf-8\") as f:\n", " actual = f.read()\n", "\n", @@ -992,74 +986,6 @@ " ), actual" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "f7649b01", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{% extends \"base.html\" %}\n", - "\n", - "{% block extrahead %}\n", - " {% set title = config.site_name %}\n", - " {% if page and page.meta and page.meta.title %}\n", - " {% set title = title ~ \" - \" ~ page.meta.title %}\n", - " {% elif page and page.title and not page.is_homepage %}\n", - " {% set title = title ~ \" - \" ~ page.title | striptags %}\n", - " {% endif %}\n", - " {% set trailing_slash = \"\" if config.site_url.endswith(\"/\") else \"/\" %}\n", - " {% set image_url = config.extra.social_image %}\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "\n", - " \n", - " \n", - " \n", - " \n", - "{% endblock %}\n", - "\n", - "{% block outdated %}\n", - " You're not viewing the latest version.\n", - " \n", - " Click here to go to latest.\n", - " \n", - "{% endblock %}\n", - "\n", - "\n", - "\n" - ] - } - ], - "source": [ - "with TemporaryDirectory() as d:\n", - " site_overrides_path = Path(d) / \"mkdocs\" / \"site_overrides\"\n", - " site_overrides_path.mkdir(exist_ok=True, parents=True)\n", - " shutil.copyfile(\n", - " Path(\"..\") / \"mkdocs\" / \"site_overrides\" / \"main.html\",\n", - " (site_overrides_path / \"main.html\"),\n", - " )\n", - "\n", - " image_url = \"https://random-domain/random-image.png\"\n", - " _update_social_image_in_site_overrides(d, image_url)\n", - "\n", - " with open((site_overrides_path / \"main.html\"), \"r\", encoding=\"utf-8\") as f:\n", - " actual = f.read()\n", - "\n", - " print(actual)\n", - "\n", - " assert \"{% set image_url = config.extra.social_image %}\" in actual, actual" - ] - }, { "cell_type": "code", "execution_count": null, @@ -1196,7 +1122,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "\u001b[31mUnexpected error: path /private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp2o74__rq/invalid_image_path.png does not exists!\u001b[0m\n" + "\u001b[31mUnexpected error: path /private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpy1rehpkv/invalid_image_path.png does not exists!\u001b[0m\n" ] }, { @@ -1230,7 +1156,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpg8lh6g5l/valid_image_path.png\n" + "/private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpyey8qlcd/valid_image_path.png\n" ] } ], @@ -1266,7 +1192,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpbyzdgiym/valid_image_path.png\n" + "/private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmppxyhbfkx/valid_image_path.png\n" ] } ], @@ -1306,7 +1232,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpzozmfb7a/mkdocs/docs_overrides/images/default_social_logo.png\n" + "/private/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpghmlcy1v/mkdocs/docs_overrides/images/default_social_logo.png\n" ] } ], @@ -1357,7 +1283,7 @@ "\n", " _update_social_image_in_mkdocs_yml(root_path, image_url)\n", "\n", - " _update_social_image_in_site_overrides(root_path, image_url)" + " _update_social_image_in_site_overrides(root_path)" ] }, { @@ -1371,7 +1297,7 @@ "output_type": "stream", "text": [ "************************************************************************************************************************\n", - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp1mgox1ev/mkdocs/docs_overrides/images/social_image.png'\n", + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpviw_z2gw/mkdocs/docs_overrides/images/social_image.png'\n", "overrides/images/social_image.png\n", "{% extends \"base.html\" %}\n", "\n", @@ -1382,7 +1308,6 @@ " {% elif page and page.title and not page.is_homepage %}\n", " {% set title = title ~ \" - \" ~ page.title | striptags %}\n", " {% endif %}\n", - " {% set trailing_slash = \"\" if config.site_url.endswith(\"/\") else \"/\" %}\n", " {% set image_url = config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image %}\n", " \n", " \n", @@ -1409,8 +1334,8 @@ "\n", "\n", "************************************************************************************************************************\n", - "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmpuomv88ni/mkdocs/docs_overrides/images/social_image.png'\n", - "https://github.com/airtai/nbdev-mkdocs/raw/main/mkdocs/docs_overrides/images/default_social_logo.png\n", + "Social share image generated and saved at: '/var/folders/6n/3rjds7v52cd83wqkd565db0h0000gn/T/tmp3j5_bo6n/mkdocs/docs_overrides/images/social_image.png'\n", + "overrides/images/social_image.png\n", "{% extends \"base.html\" %}\n", "\n", "{% block extrahead %}\n", @@ -1420,8 +1345,7 @@ " {% elif page and page.title and not page.is_homepage %}\n", " {% set title = title ~ \" - \" ~ page.title | striptags %}\n", " {% endif %}\n", - " {% set trailing_slash = \"\" if config.site_url.endswith(\"/\") else \"/\" %}\n", - " {% set image_url = config.extra.social_image %}\n", + " {% set image_url = config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image %}\n", " \n", " \n", " \n", @@ -1487,29 +1411,17 @@ " yaml = YAML()\n", " config = yaml.load((Path(d) / \"mkdocs/mkdocs.yml\"))\n", " print(config[\"extra\"][\"social_image\"])\n", - "\n", - " if generator == \"file\":\n", - " assert (\n", - " config[\"extra\"][\"social_image\"] == \"overrides/images/social_image.png\"\n", - " ), config[\"extra\"][\"social_image\"]\n", - " elif generator == \"dall_e\":\n", - " assert (\n", - " config[\"extra\"][\"social_image\"]\n", - " == \"https://github.com/airtai/nbdev-mkdocs/raw/main/mkdocs/docs_overrides/images/default_social_logo.png\"\n", - " ), config[\"extra\"][\"social_image\"]\n", - "\n", + " assert (\n", + " config[\"extra\"][\"social_image\"] == \"overrides/images/social_image.png\"\n", + " ), config[\"extra\"][\"social_image\"]\n", + " \n", " with open((site_overrides_path / \"main.html\"), \"r\", encoding=\"utf-8\") as f:\n", " actual = f.read()\n", - "\n", " print(actual)\n", - "\n", - " if generator == \"file\":\n", - " assert (\n", - " '{% set image_url = config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image %}'\n", - " in actual\n", - " ), actual\n", - " elif generator == \"dall_e\":\n", - " assert \"{% set image_url = config.extra.social_image %}\" in actual, actual" + " assert (\n", + " '{% set image_url = config.site_url ~ (\"\" if config.site_url.endswith(\"/\") else \"/\") ~ config.extra.social_image %}'\n", + " in actual\n", + " ), actual" ] }, { diff --git a/nbs/guides/Setting_up_social_cards.ipynb b/nbs/guides/Setting_up_social_cards.ipynb index 82ab91a..c0462d9 100644 --- a/nbs/guides/Setting_up_social_cards.ipynb +++ b/nbs/guides/Setting_up_social_cards.ipynb @@ -68,7 +68,8 @@ "\u001b[2m│\u001b[0m machine in the social share image. \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[2m[default: file] \u001b[0m \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[1;36m-\u001b[0m\u001b[1;36m-prompt\u001b[0m \u001b[1;33mTEXT \u001b[0m The prompt to use for generating the \u001b[2m│\u001b[0m\r\n", - "\u001b[2m│\u001b[0m image. \u001b[2m│\u001b[0m\r\n", + "\u001b[2m│\u001b[0m image. Please pass this option only when \u001b[2m│\u001b[0m\r\n", + "\u001b[2m│\u001b[0m using 'dall_e' as the generator. \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[2m[default: Cute animal wearing hoodie \u001b[0m \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[2msitting in high chair in purple room, \u001b[0m \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[2mbrowsing computer, 3d render] \u001b[0m \u001b[2m│\u001b[0m\r\n", @@ -76,7 +77,9 @@ "\u001b[2m│\u001b[0m share image. Use images with a 1:1 aspect \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m ratio and at least 512x512 pixels for the \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m best results. If None, then the default \u001b[2m│\u001b[0m\r\n", - "\u001b[2m│\u001b[0m image will be used. \u001b[2m│\u001b[0m\r\n", + "\u001b[2m│\u001b[0m image will be used. Please pass this \u001b[2m│\u001b[0m\r\n", + "\u001b[2m│\u001b[0m option only when using 'file' as the \u001b[2m│\u001b[0m\r\n", + "\u001b[2m│\u001b[0m generator. \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[2m[default: None] \u001b[0m \u001b[2m│\u001b[0m\r\n", "\u001b[2m│\u001b[0m \u001b[1;36m-\u001b[0m\u001b[1;36m-help\u001b[0m \u001b[1;33m \u001b[0m Show this message and exit. \u001b[2m│\u001b[0m\r\n", "\u001b[2m╰──────────────────────────────────────────────────────────────────────────────╯\u001b[0m\r\n",