Skip to content

Commit

Permalink
fixed page breaks and extra spaces between various status-codes with …
Browse files Browse the repository at this point in the history
…missing body
  • Loading branch information
mrin9 committed Sep 12, 2019
1 parent cdbfa81 commit 5f91c84
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 75 deletions.
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
https://assets.zuora.com/zuora-documentation/swagger.yaml <<< Large spec with lot of markdown
-->


<rapi-pdf id='rapipdf' spec-url="./examples/specs/test.json" include-info="false" include-security="false" include-toc="false"> </rapi-pdf>
<!--
<rapi-pdf id='rapipdf' spec-url="https://api.apis.guru/v2/specs/bitbucket.org/2.0/swagger.json" include-info="false" include-security="false" include-toc="false"> </rapi-pdf>
<rapi-pdf id='rapipdf' spec-url="./examples/specs/test.json" include-info="false" include-security="false" include-toc="false"> </rapi-pdf>
-->
<rapi-pdf id='rapipdf' spec-url="https://petstore.swagger.io/v2/swagger.json"> </rapi-pdf>


<button onclick="changeSpec()">Change Spec</button>
<script type="text/javascript">
Expand Down
65 changes: 20 additions & 45 deletions src/utils/common-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,7 @@ export function schemaToPdf (schema, obj=[], name, level=0) {
{
colSpan: 3,
stack:[
(name ? {
text:[
{text:name, style:['small', 'mono', 'blue']},
{text:` {`, style:['small', 'mono', 'blue']},
],
}
:{text:`root { `, style:['small', 'mono', 'blue']}),
{text:`${name?name:'object'} { `, style:['small', 'mono', 'blue']},
{text:(schema.description ? schema.description : ''), style:['sub', 'blue'],margin:[0,2,0,0]},
{
margin: [10, 0, 0, 0],
Expand Down Expand Up @@ -257,13 +251,7 @@ export function schemaToPdf (schema, obj=[], name, level=0) {
{
colSpan: 3,
stack:[
{
text:[
{text:name, style:['small', 'mono', 'blue']},
{text:`${typeOfArr==='object'?'[{':'['}`, style:['small', 'mono', (typeOfArr === 'object'?'blue':'lightGray') ]},
],
margin:0
},
{text:`${name?name:'array '} ${typeOfArr==='object'?'[{':'['}` , style:['small', 'mono', (typeOfArr === 'object'?'blue':'lightGray')], margin:0},
{text:(schema.description ? schema.description : ''), style:['small','lightGray'],margin:[0,2,0,0]},
{
margin: [10, 0, 0, 0],
Expand All @@ -281,7 +269,7 @@ export function schemaToPdf (schema, obj=[], name, level=0) {
}
else{
obj = [
{text:name, style:['small', 'mono', 'blue'],margin:0},
{text:name, style:['small', 'mono'],margin:0},
{text:`[${typeOfArr}]`, style:['small','mono', 'lightGray'], margin:0},
{text:(schema.description?schema.description:''), style:['small', 'lightGray'],margin:[0,2,0,0]}
];
Expand All @@ -303,37 +291,24 @@ export function schemaToPdf (schema, obj=[], name, level=0) {
}
});

// This is problematic
if (allOfRows.length >= 1){
obj = [{
colSpan: 3,
stack:[
{text:`${name?name:'root'} {`, style:['small', 'mono', 'blue']},
{text:(schema.description ? schema.description : ''), style:['sub', 'blue'],margin:[0,2,0,0]},
{
margin: [10, 0, 0, 0],
widths: [ 'auto', 'auto', '*' ],
layout: rowLinesOnlyTableLayout,
table: {
dontBreakRows: true,
body: allOfRows
}
},
{text:`}`, style:['small', 'mono', 'blue']}
]
}]
}

/*
obj = [
{text:name,style:['small', 'mono'],margin:0},
{text:(schema.type ? schema.type:''), style:['small', 'mono', 'lightGray'],margin:0},
{text:(schema.description?schema.description:''), style:['small', 'lightGray'], margin:[0,2,0,0]}
];
*/

obj = [{
colSpan: 3,
stack:[
{text:`${name?name:'object'} {`, style:['small', 'mono', 'blue']},
{text:(schema.description ? schema.description : ''), style:['sub', 'blue'],margin:[0,2,0,0]},
{
margin: [10, 0, 0, 0],
widths: [ 'auto', 'auto', '*' ],
layout: rowLinesOnlyTableLayout,
table: {
dontBreakRows: true,
body: allOfRows
}
},
{text:`}`, style:['small', 'mono', 'blue']}
]
}]


}

// Primitive Type (String, Integer, Boolean etc)
Expand Down
54 changes: 30 additions & 24 deletions src/utils/pdf-gen-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getInfoDef(spec, bookTitle, localize){
contactEmail,
contactUrl,
termsOfService
]}
]},
]
}

Expand All @@ -47,13 +47,14 @@ export function getInfoDef(spec, bookTitle, localize){
(spec.info.title ? {text:spec.info.title, style:['title', 'right']} : ''),
(spec.info.version ? {text:`${localize.apiVersion}: ${spec.info.version}`, style:['p','b', 'right', 'alternate']} : ''),
specInfDescrMarkDef,
...contactDef
...contactDef,
{text:'', pageBreak:'after'},
];

}
else{
content = [
{text:bookTitle?bookTitle:apiVersion.apiReference, style:['h1', 'bold', 'primary','right', 'topMargin1']}
{text:bookTitle?bookTitle:apiVersion.apiReference, style:['h1', 'bold', 'primary','right', 'topMargin1']},
{text:'', pageBreak:'after'},
];
}
return content;
Expand All @@ -63,7 +64,7 @@ export function getInfoDef(spec, bookTitle, localize){
export function getSecurityDef(spec, tableLayout, localize){
let content =[];
if (spec.securitySchemes){
content.push( {text:localize.securityAndAuthentication, style:['h3', 'b', 'primary','right', 'topMargin3'], pageBreak:'before'} );
content.push( {text:localize.securityAndAuthentication, style:['h3', 'b', 'primary','right', 'topMargin3']} );
content.push({text:localize.securitySchemes, style:['b','tableMargin']});
let tableContent = [
[ {text: localize.type, style: ['small','b']}, {text: localize.description, style: ['small','b']} ]
Expand All @@ -81,15 +82,17 @@ export function getSecurityDef(spec, tableLayout, localize){
body: tableContent,
},
layout: tableLayout,
style: 'tableMargin'
style: 'tableMargin',
pageBreak:'after'
});

}
return content;
}

// API details def
export function getApiDef(spec, filterPath, sectionHeading, tableLayout, localize){
let content =[{text: sectionHeading, style:['h2','b'],pageBreak:'before'}];
let content =[{text: sectionHeading, style:['h2','b']}];
let tagSeq=0;

// Sort by Tag name (allready sorted)
Expand Down Expand Up @@ -212,14 +215,14 @@ export function getApiDef(spec, filterPath, sectionHeading, tableLayout, localiz
tocMargin: [0, 10, 0, 0],
},
tagDescrMarkDef,
operationContent
operationContent,
{text:'', pageBreak:'after'}
);
}

});

//content.push({text:'', pageBreak:'after'});
return content;

}


Expand Down Expand Up @@ -276,9 +279,9 @@ function getParameterTableDef(parameters, paramType, tableLayout, localize){
}
let tableContent = [
[
{text: localize.name, style: ['sub','b','alternate']},
{text: localize.type, style: ['sub','b','alternate']},
{text: localize.description, style: ['sub','b','alternate']}
{text: localize.name, style: ['sub', 'b', 'alternate']},
{text: localize.type, style: ['sub', 'b', 'alternate']},
{text: localize.description, style: ['sub', 'b', 'alternate']}
]
];

Expand All @@ -292,9 +295,9 @@ function getParameterTableDef(parameters, paramType, tableLayout, localize){
type = "array of " + param.items.type;
}
tableContent.push([
{ text:paramName, style:['small','mono'] },
{ text:type + format, style:['small','mono'] },
{ text:param.description, style:['small'],margin:[0,2,0,0]},
{ text:paramName, style:['small', 'mono'] },
{ text:type + format, style:['small', 'mono' ] },
{ text:param.description, style:['small'], margin:[0,2,0,0]},
]);
}

Expand All @@ -312,11 +315,11 @@ function getParameterTableDef(parameters, paramType, tableLayout, localize){
},
{
stack:[
{ text: `${paramSchema.type==='array' ? paramSchema.arrayType:paramSchema.type}${paramSchema.format ? `(${paramSchema.format})`:'' }`, style:['small','mono']},
{ text: `${paramSchema.type==='array' ? paramSchema.arrayType:paramSchema.type}${paramSchema.format ? `(${paramSchema.format})`:'' }`, style:['small','mono', 'gray']},
( paramSchema.constrain ? { text: paramSchema.constrain, style:['small', 'gray']}:''),
( paramSchema.allowedValues ? { text:[
{text: localize.allowed+': ', style:['b','small']},
{text: paramSchema.allowedValues, style:['small', 'gray']}
{text: paramSchema.allowedValues, style:['small']}
]} : ''
),
( paramSchema.pattern ? { text: `${localize.pattern}: ${paramSchema.pattern}`, style:['small','gray']}:''),
Expand Down Expand Up @@ -355,7 +358,7 @@ function getResponseDef(responses, tableLayout, localize){
origSchema = JSON.parse(JSON.stringify(origSchema, removeCircularReferences()));
reponseModelTableDef = schemaToPdf(origSchema);
if (reponseModelTableDef && reponseModelTableDef[0] && reponseModelTableDef[0].stack){
reponseModelTableDef[0].colSpan=undefined;
reponseModelTableDef[0].colSpan = undefined;
reponseModelTableDef = {
margin:[0,5,0,0],
//layout:tableLayout,
Expand All @@ -381,16 +384,19 @@ function getResponseDef(responses, tableLayout, localize){
margin:[0,10,0,0]
});

allResponseModelTabelDefs.map(function(respModelTableDef){
respDef.push(respModelTableDef);
})
if (responses[statusCode].content){
allResponseModelTabelDefs.map(function(respModelTableDef){
respDef.push(respModelTableDef);
})
}
}

return respDef;
}

//API List Def
export function getApiListDef(spec, sectionHeading, tableLayout, localize) {
let content =[{text: sectionHeading, style:['h3','b'],pageBreak:'before'}];
let content =[{text: sectionHeading, style:['h3','b'],pageBreak:'none'}];
spec.tags.map(function(tag, i){
let tableContent = [
[ {text: localize.method, style: ['small','b']}, {text: localize.api, style: ['small','b']}]
Expand All @@ -411,7 +417,7 @@ export function getApiListDef(spec, sectionHeading, tableLayout, localize) {
});

content.push(
{text: tag.name, style:['h6','b','primary','tableMargin'], pageBreak: i === 0 ? 'none' : 'before'},
{text: tag.name, style:['h6','b','primary','tableMargin'], pageBreak: i === 0 ? 'none' : 'after'},
{text: tag.description, style:['p']},
{
table: {
Expand Down
7 changes: 4 additions & 3 deletions src/utils/pdf-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export default async function createPdf(specUrl, options){
toc: {
title: {text: options.localize.index, style:['b', 'h2']},
numberStyle: {bold: true},
style:['small'],
}
style:['small']
},
pageBreak:'after'
};
allContent.push({text:'', pageBreak:'after'});
// allContent.push({text:'', pageBreak:'after'});
allContent.push(tocDef);
}
if (options.includeSecurity){
Expand Down

0 comments on commit 5f91c84

Please sign in to comment.