{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b0bd990-31b6-43f5-a107-50ce31ae0561",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# this notebook is used to generate corpus/docs.csv\n",
    "# to run this notebook, you should clone these repos into repos directory:\n",
    "# - git@github.com:pingcap/docs-staging.git\n",
    "\n",
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d50ffc36-5a0a-4206-af4b-a5944c89b4e8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def get_doc_df(doc_dir, link_prefix):\n",
    "    if not doc_dir.endswith('/'):\n",
    "        doc_dir += '/'\n",
    "    if not link_prefix.endswith('/'):\n",
    "        link_prefix += '/'\n",
    "\n",
    "    xs = []\n",
    "    for root, _, files in os.walk(doc_dir):\n",
    "        if '/.' in root:\n",
    "            continue\n",
    "        for file in files:\n",
    "            if file.startswith('_') or not file.endswith('.md'):\n",
    "                continue\n",
    "            link = f'{link_prefix}{file[:-3]}'\n",
    "            with open(os.path.join(root, file)) as f:\n",
    "                docs = []\n",
    "                for doc in f.read().split('\\n'):\n",
    "                    if len(doc.strip()) > 0: \n",
    "                        docs.append(doc)\n",
    "                for i, doc in enumerate(docs):\n",
    "                    xs.append({\n",
    "                        'title': file,\n",
    "                        'link': link,\n",
    "                        'idx': i,\n",
    "                        'content': doc\n",
    "                    })\n",
    "    return pd.DataFrame.from_records(xs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9304af53-abc8-425b-8b35-5b3535056ac8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = get_doc_df(\n",
    "    'repos/docs-staging/markdown-pages/en/tidbcloud', \n",
    "    'https://docs.pingcap.com/tidbcloud/',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3daa6fc8-857e-4ee7-9b91-2f297ce341ee",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df['source'] = 'tidbcloud'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9c7aad1-a887-441d-b82d-e78aace8f564",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df.to_csv('corpus/tidbcloud.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
