Skip to content

Commit

Permalink
Implement an early logic for fetching documents by ids #4 and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Aadv1k committed Jan 9, 2024
1 parent 872f584 commit c9a48b1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.gdoc
*/__pycache__
11 changes: 0 additions & 11 deletions TODO.md

This file was deleted.

32 changes: 32 additions & 0 deletions autoload/gdoc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

8 changes: 7 additions & 1 deletion plugin/gdoc.vim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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, <q-args>)
14 changes: 14 additions & 0 deletions python/google_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c9a48b1

Please sign in to comment.