Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding inline link support and fixing list issues #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
6. You will get an email at your Google Account containing the HTML
output of the Google Doc with inline images.

[1]: https://raw.githubusercontent.com/oazabir/GoogleDoc2Html/master/code.js
[1]: https://github.com/trjohnst/GoogleDoc2Html/raw/master/code.js
107 changes: 50 additions & 57 deletions code.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ function processItem(item, listCounters, images) {
if (gt === DocumentApp.GlyphType.BULLET
|| gt === DocumentApp.GlyphType.HOLLOW_BULLET
|| gt === DocumentApp.GlyphType.SQUARE_BULLET) {
prefix = '<ul><li>', suffix = "</li>";

suffix += "</ul>";
prefix = '<ul><li>', suffix = "</li>";
}
else {
// Ordered list (<ol>):
Expand All @@ -113,7 +111,12 @@ function processItem(item, listCounters, images) {
suffix = "</li>";
}

if (item.isAtDocumentEnd() || (item.getNextSibling() && (item.getNextSibling().getType() != DocumentApp.ElementType.LIST_ITEM))) {
var isAtEnd = item.isAtDocumentEnd();
var nextSibling = item.getNextSibling();
var listItemType = DocumentApp.ElementType.LIST_ITEM;
var isNextAtEndOfList = (!nextSibling || (nextSibling && (nextSibling.getType() != listItemType)));

if (isAtEnd || isNextAtEndOfList) {
if (gt === DocumentApp.GlyphType.BULLET
|| gt === DocumentApp.GlyphType.HOLLOW_BULLET
|| gt === DocumentApp.GlyphType.SQUARE_BULLET) {
Expand Down Expand Up @@ -159,69 +162,59 @@ function processText(item, output) {
var text = item.getText();
var indices = item.getTextAttributeIndices();

if (indices.length <= 1) {
// Assuming that a whole para fully italic is a quote
if(item.isBold()) {
output.push('<strong>' + text + '</strong>');
for (var i=0; i < indices.length; i ++) {
var partAtts = item.getAttributes(indices[i]);
var startPos = indices[i];
var endPos = i+1 < indices.length ? indices[i+1]: text.length;
var partText = text.substring(startPos, endPos);

Logger.log(partText);


if (partAtts.LINK_URL) {
output.push('<a href="' + partAtts.LINK_URL + '">');
}
if (partAtts.ITALIC) {
output.push('<i>');
}
if (partAtts.BOLD) {
output.push('<strong>');
}
if (partAtts.UNDERLINE && !partAtts.LINK_URL) {
output.push('<u>');
}
else if(item.isItalic()) {
output.push('<blockquote>' + text + '</blockquote>');

// If someone has written [xxx] and made this whole text some special font, like superscript
// then treat it as a reference and make it superscript.
// Unfortunately in Google Docs, there's no way to detect superscript
if (partText.indexOf('[')==0 && partText[partText.length-1] == ']') {
output.push('<sup>' + partText + '</sup>');
}
else if (text.trim().indexOf('http://') == 0) {
output.push('<a href="' + text + '" rel="nofollow">' + text + '</a>');
else if (partText.trim().indexOf('http://') == 0) {
output.push('<a href="' + partText + '" rel="nofollow">' + partText + '</a>');
}
else {
output.push(text);
output.push(partText);
}
}
else {

for (var i=0; i < indices.length; i ++) {
var partAtts = item.getAttributes(indices[i]);
var startPos = indices[i];
var endPos = i+1 < indices.length ? indices[i+1]: text.length;
var partText = text.substring(startPos, endPos);

Logger.log(partText);

if (partAtts.ITALIC) {
output.push('<i>');
}
if (partAtts.BOLD) {
output.push('<strong>');
}
if (partAtts.UNDERLINE) {
output.push('<u>');
}

// If someone has written [xxx] and made this whole text some special font, like superscript
// then treat it as a reference and make it superscript.
// Unfortunately in Google Docs, there's no way to detect superscript
if (partText.indexOf('[')==0 && partText[partText.length-1] == ']') {
output.push('<sup>' + partText + '</sup>');
}
else if (partText.trim().indexOf('http://') == 0) {
output.push('<a href="' + partText + '" rel="nofollow">' + partText + '</a>');
}
else {
output.push(partText);
}

if (partAtts.ITALIC) {
output.push('</i>');
}
if (partAtts.BOLD) {
output.push('</strong>');
}
if (partAtts.UNDERLINE) {
output.push('</u>');
}


if (partAtts.UNDERLINE && !partAtts.LINK_URL) {
output.push('</u>');
}
if (partAtts.BOLD) {
output.push('</strong>');
}
if (partAtts.ITALIC) {
output.push('</i>');
}
if (partAtts.LINK_URL) {
output.push('</a>');
}

}
}



function processImage(item, images, output)
{
images = images || [];
Expand Down