-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Eng
committed
Jan 5, 2018
1 parent
8dff725
commit 3282d13
Showing
48 changed files
with
1,120 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/services/src/main/java/org/zanata/adapter/AsciidocAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2017, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.zanata.adapter; | ||
|
||
import org.zanata.adapter.asciidoc.AsciidocFilter; | ||
|
||
/** | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
*/ | ||
public class AsciidocAdapter extends OkapiFilterAdapter { | ||
|
||
public AsciidocAdapter() { | ||
super(prepareFilter(), IdSource.contentHash); | ||
} | ||
|
||
private static AsciidocFilter prepareFilter() { | ||
return new AsciidocFilter(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
server/services/src/main/java/org/zanata/adapter/asciidoc/AsciidocEventBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.zanata.adapter.asciidoc; | ||
|
||
import net.sf.okapi.common.filters.EventBuilder; | ||
import net.sf.okapi.common.filters.IFilter; | ||
import net.sf.okapi.common.filters.InlineCodeFinder; | ||
import net.sf.okapi.common.resource.ITextUnit; | ||
import net.sf.okapi.common.resource.TextFragment; | ||
/** | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
*/ | ||
public class AsciidocEventBuilder extends EventBuilder { | ||
private InlineCodeFinder codeFinder; | ||
|
||
public AsciidocEventBuilder(String rootId, IFilter subFilter) { | ||
super(rootId, subFilter); | ||
codeFinder = null; | ||
} | ||
|
||
@Override | ||
protected ITextUnit postProcessTextUnit(ITextUnit textUnit) { | ||
TextFragment text = textUnit.getSource().getFirstContent(); | ||
if (codeFinder != null) { | ||
codeFinder.process(text); | ||
} | ||
return textUnit; | ||
} | ||
|
||
public void setCodeFinder(InlineCodeFinder codeFinder) { | ||
this.codeFinder = codeFinder; | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
server/services/src/main/java/org/zanata/adapter/asciidoc/AsciidocFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package org.zanata.adapter.asciidoc; | ||
|
||
import net.sf.okapi.common.BOMNewlineEncodingDetector; | ||
import net.sf.okapi.common.Event; | ||
import net.sf.okapi.common.IParameters; | ||
import net.sf.okapi.common.exceptions.OkapiBadFilterInputException; | ||
import net.sf.okapi.common.exceptions.OkapiUnsupportedEncodingException; | ||
import net.sf.okapi.common.filters.AbstractFilter; | ||
import net.sf.okapi.common.filters.FilterConfiguration; | ||
import net.sf.okapi.common.resource.RawDocument; | ||
import net.sf.okapi.filters.json.Parameters; | ||
import net.sf.okapi.filters.json.parser.ParseException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
/** | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
*/ | ||
public class AsciidocFilter extends AbstractFilter { | ||
private static final String MIME_TYPE = "application/asciidoc"; | ||
private static final Logger log = LoggerFactory.getLogger(AsciidocFilter.class); | ||
|
||
private BOMNewlineEncodingDetector detector; | ||
private RawDocument currentRawDocument; | ||
private AsciidocEventBuilder eventBuilder; | ||
private Parameters params; | ||
private AsciidocParser parser; | ||
|
||
public AsciidocFilter() { | ||
super(); | ||
setMimeType(MIME_TYPE); | ||
setMultilingual(false); | ||
setName("okf_asciidoc"); | ||
setDisplayName("Asciidoc Filter"); | ||
|
||
addConfiguration(new FilterConfiguration( | ||
getName(), | ||
MIME_TYPE, | ||
getClass().getName(), | ||
"Asciidoc", | ||
"Asciidoc files", | ||
null, | ||
".adoc")); | ||
setParameters(new Parameters()); | ||
} | ||
|
||
@Override | ||
protected boolean isUtf8Encoding() { | ||
return detector != null && detector.hasUtf8Encoding(); | ||
} | ||
|
||
@Override | ||
protected boolean isUtf8Bom() { | ||
return detector != null && detector.hasUtf8Bom(); | ||
} | ||
|
||
@Override | ||
public void open(RawDocument input) { | ||
open(input, true); | ||
} | ||
|
||
@Override | ||
public void open(RawDocument input, boolean generateSkeleton) { | ||
currentRawDocument = input; | ||
|
||
detector = new BOMNewlineEncodingDetector(input.getStream(), input.getEncoding()); | ||
detector.detectAndRemoveBom(); | ||
setNewlineType(detector.getNewlineType().toString()); | ||
|
||
String encoding = getDetectedEncoding(); | ||
input.setEncoding(encoding); | ||
setEncoding(encoding); | ||
setOptions(input.getSourceLocale(), input.getTargetLocale(), encoding, generateSkeleton); | ||
|
||
BufferedReader reader; | ||
try { | ||
reader = new BufferedReader(new InputStreamReader(detector.getInputStream(), encoding)); | ||
} | ||
catch (UnsupportedEncodingException e) { | ||
throw new OkapiUnsupportedEncodingException( | ||
String.format("The encoding '%s' is not supported.", encoding), e); | ||
} | ||
|
||
if (input.getInputURI() != null) { | ||
setDocumentName(input.getInputURI().getPath()); | ||
} | ||
|
||
// Create EventBuilder with document name as rootId | ||
if (eventBuilder == null) { | ||
eventBuilder = new AsciidocEventBuilder(getParentId(), this); | ||
} else { | ||
eventBuilder.reset(getParentId(), this); | ||
} | ||
eventBuilder.setPreserveWhitespace(true); | ||
|
||
// Compile code finder rules | ||
if (params.getUseCodeFinder()) { | ||
params.getCodeFinder().compile(); | ||
eventBuilder.setCodeFinder(params.getCodeFinder()); | ||
} | ||
|
||
eventBuilder.addFilterEvent(createStartFilterEvent()); | ||
this.parser = new AsciidocParser(reader, eventBuilder); | ||
try { | ||
parser.parse(); | ||
} catch (ParseException e) { | ||
String error = String.format("Error parsing ASCIIDOC file: %s", e.getMessage()); | ||
log.error(error, e); | ||
throw new OkapiBadFilterInputException(error, e); | ||
} | ||
|
||
eventBuilder.flushRemainingTempEvents(); | ||
eventBuilder.addFilterEvent(createEndFilterEvent()); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return eventBuilder.hasNext(); | ||
} | ||
|
||
@Override | ||
public Event next() { | ||
return eventBuilder.next(); | ||
} | ||
|
||
@Override | ||
public IParameters getParameters() { | ||
return params; | ||
} | ||
|
||
@Override | ||
public void setParameters(IParameters iParameters) { | ||
this.params = (Parameters)iParameters; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (currentRawDocument != null) { | ||
currentRawDocument.close(); | ||
detector = null; | ||
eventBuilder = null; | ||
} | ||
} | ||
|
||
private String getDetectedEncoding() { | ||
String detectedEncoding = getEncoding(); | ||
if (detector.isDefinitive()) { | ||
detectedEncoding = detector.getEncoding(); | ||
log.debug("Overriding user set encoding (if any). Setting auto-detected encoding {}.", | ||
detectedEncoding); | ||
|
||
} else if (!detector.isDefinitive() && getEncoding().equals(RawDocument.UNKOWN_ENCODING)) { | ||
detectedEncoding = detector.getEncoding(); | ||
log.debug("Default encoding and detected encoding not found. Using best guess encoding {}", | ||
detectedEncoding); | ||
} | ||
return detectedEncoding; | ||
} | ||
} |
Oops, something went wrong.