From c9a48b15fd538d15324a6500a29bfa439061e375 Mon Sep 17 00:00:00 2001 From: aadv1k Date: Tue, 9 Jan 2024 16:34:12 +0530 Subject: [PATCH] Implement an early logic for fetching documents by ids #4 and #2 --- .gitignore | 1 + TODO.md | 11 ----------- autoload/gdoc.vim | 32 ++++++++++++++++++++++++++++++++ plugin/gdoc.vim | 8 +++++++- python/google_api.py | 14 ++++++++++++++ 5 files changed, 54 insertions(+), 12 deletions(-) delete mode 100644 TODO.md diff --git a/.gitignore b/.gitignore index 70ba93d..e4365f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .gdoc +*/__pycache__ diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 3f03940..0000000 --- a/TODO.md +++ /dev/null @@ -1,11 +0,0 @@ -### Converting formats like markdown to google documents -Unfortunately as of now, there isn't really an official solution that can -convert markdown to the very distinct format that google documents uses. I've -looked into pandoc and other solutions but none of them are usable in this -setup. - -I have also tried hacking up a custom "compiler" but yet again, the speeds -alongside the complexity don't make much sense. - -### Moving documents to specific folders -- This is a TODO diff --git a/autoload/gdoc.vim b/autoload/gdoc.vim index 26c9218..5260303 100755 --- a/autoload/gdoc.vim +++ b/autoload/gdoc.vim @@ -11,6 +11,12 @@ function gdoc#LoadCommand(plug_path, path_to_creds, token_directory, gdoc_path, call gdoc#SyncDoc() elseif mode == 'rm' call gdoc#RmDoc() + elseif mode == 'fetch-doc' + if len(args) <= 1 + echoerr "gdoc.vim: fetch-doc expects a document id" + else + call gdoc#FetchDoc(args[1]) + endif else echoerr "Exaustive handling of Arguments; " . mode . " Not found" endif @@ -162,3 +168,29 @@ else: raise GdocErr('[gdoc.vim] Something went wrong.') EOF endfunction + + +function gdoc#FetchDoc(doc_id) +python3 << EOF + +try: + content = query.read_doc(document_id) +except: + raise GdocErr(f"Was unable to read document of id '{document_id}' perhaps its invalid?") + +extracted_text = query.parse_doc(content)[0] +lines = extracted_text.split('\n') + +for line_number, line_content in enumerate(lines): + vim.command(f"call setline({line_number + 1}, '{line_content}')") + +vim.command("write!") + +target_file_path = vim.eval("expand('%:p')") + +query.write_id_to_file(document_id, target_file_path) + +print(f"gdoc.vim: local association created for {document_id}") +EOF +endfunction + diff --git a/plugin/gdoc.vim b/plugin/gdoc.vim index cde524e..3d3ada2 100755 --- a/plugin/gdoc.vim +++ b/plugin/gdoc.vim @@ -1,3 +1,9 @@ + + +let g:path_to_creds = '~/.vim/credentials.json' " this is required +let g:gdoc_file_path = '~/.vim/' " optional; default is ./ +let g:token_directory = '~/.vim/' " optional; default is ./ + if !has('python3') echoerr "[gdoc.vim] Python3 is required for gdoc.vim to work." finish @@ -15,7 +21,7 @@ if path_to_creds == -1 endif function! GdocComplete(ArgLead, CmdLine, CursorPos) - return ['sync', 'sync-doc', 'write', 'rm'] + return ['sync', 'sync-doc', 'write', 'rm', 'fetch-doc'] endfunction command! -nargs=+ -complete=customlist,GdocComplete Gdoc call gdoc#LoadCommand(plug_path, path_to_creds, token_directory, gdoc_path, ) diff --git a/python/google_api.py b/python/google_api.py index 2e0c5e5..d74f556 100755 --- a/python/google_api.py +++ b/python/google_api.py @@ -104,6 +104,20 @@ def read_doc(self, doc_id): document = self.docs_service.documents().get(documentId=doc_id).execute() return document + def extract_text_from_gdoc_data(self, data): + text_content = "" + + if 'body' in json_data and 'content' in json_data['body']: + for item in json_data['body']['content']: + if 'paragraph' in item and 'elements' in item['paragraph']: + for element in item['paragraph']['elements']: + if 'textRun' in element and 'content' in element['textRun']: + text_content += element['textRun']['content'] + return text_content + + + + def edit_doc(self, doc_id, blob): result = self.docs_service.documents().batchUpdate( documentId=doc_id, body={'requests': blob}).execute()