Skip to content

Commit 78c07ae

Browse files
committed
fix indentions
1 parent 0dbcfe9 commit 78c07ae

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

docs/_plugins/include_example.rb

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,54 @@ def render(context)
2424
Pygments.highlight(code, :lexer => @lang)
2525
end
2626

27-
# Select lines according to labels in code. Currently we use "begin code" and "end code" as
28-
# labels.
27+
# Trim the code block so as to have the same indention, regardless of their positions in the
28+
# code file.
29+
def trim_codeblock(lines)
30+
# Select the minimum indention of the current code block.
31+
min_start_spaces = lines
32+
.select{ |l| l.strip.size !=0 }
33+
.map{ |l| l[/\A */].size }
34+
.min
35+
36+
lines.map{ |l| l[min_start_spaces .. -1] }
37+
end
38+
39+
# Select lines according to labels in code. Currently we use "$example on$" and "$example off$"
40+
# as labels. Note that code blocks identified by the labels should not overlap.
2941
def select_lines(code)
3042
lines = code.each_line.to_a
31-
start = lines.each_with_index.select { |l, i| l.include? "$example on$" }.last.last
32-
endline = lines.each_with_index.select { |l, i| l.include? "$example off$" }.last.last
33-
length = lines.count
34-
35-
if start > 0 or endline < length - 1
36-
raise "Wrong positions of begin and end tags." if start > endline
37-
range = Range.new(start, endline)
38-
code = lines[range].join
43+
44+
# Select the array of start labels from code.
45+
startIndices = lines
46+
.each_with_index
47+
.select{ |l, i| l.include? "$example on$" }
48+
.map{ |l, i| i }
49+
50+
# Select the array of end labels from code.
51+
endIndices = lines
52+
.each_with_index
53+
.select{ |l, i| l.include? "$example off$" }
54+
.map{ |l, i| i }
55+
56+
raise "Start indices amount is not equal to end indices amount, please check the code." \
57+
unless startIndices.size == endIndices.size
58+
59+
raise "No code is selected by include_example, please check the code." \
60+
if startIndices.size == 0
61+
62+
# Select and join code blocks together, with a space line between each of two continuous
63+
# blocks.
64+
lastIndex = -1
65+
result = ""
66+
startIndices.zip(endIndices).each do |start, endline|
67+
raise "Overlapping between two example code blocks are not allowed." if start <= lastIndex
68+
raise "$example on$ should not be in the same line with $example off$." if start == endline
69+
lastIndex = endline
70+
range = Range.new(start + 1, endline - 1)
71+
result += trim_codeblock(lines[range]).join
72+
result += "\n"
3973
end
40-
code
74+
result
4175
end
4276
end
4377
end

examples/src/main/java/org/apache/spark/examples/ml/JavaCrossValidatorExample.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.spark.examples.ml;
1919

20+
// $example on$
2021
import java.util.List;
2122

2223
import com.google.common.collect.Lists;
@@ -36,6 +37,7 @@
3637
import org.apache.spark.sql.DataFrame;
3738
import org.apache.spark.sql.Row;
3839
import org.apache.spark.sql.SQLContext;
40+
// $example off$
3941

4042
/**
4143
* A simple example demonstrating model selection using CrossValidator.

examples/src/main/python/ml/cross_validator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
from __future__ import print_function
1919

20+
# $example on$
2021
from pyspark import SparkContext
2122
from pyspark.ml import Pipeline
2223
from pyspark.ml.classification import LogisticRegression
2324
from pyspark.ml.evaluation import BinaryClassificationEvaluator
2425
from pyspark.ml.feature import HashingTF, Tokenizer
2526
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
2627
from pyspark.sql import Row, SQLContext
28+
# $example off$
2729

2830
"""
2931
A simple example demonstrating model selection using CrossValidator.

examples/src/main/scala/org/apache/spark/examples/ml/CrossValidatorExample.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// scalastyle:off println
1919
package org.apache.spark.examples.ml
2020

21+
// $example on$
2122
import org.apache.spark.{SparkConf, SparkContext}
2223
import org.apache.spark.ml.Pipeline
2324
import org.apache.spark.ml.classification.LogisticRegression
@@ -26,6 +27,7 @@ import org.apache.spark.ml.feature.{HashingTF, Tokenizer}
2627
import org.apache.spark.ml.tuning.{ParamGridBuilder, CrossValidator}
2728
import org.apache.spark.mllib.linalg.Vector
2829
import org.apache.spark.sql.{Row, SQLContext}
30+
// $example off$
2931

3032
/**
3133
* A simple example demonstrating model selection using CrossValidator.

0 commit comments

Comments
 (0)