AsciiDoctorHeadline implements AsciidoctorTextSelectable { static String calculateId(String name) { if (name==null){ return ""; } String id = REGEXP.matcher(name).replaceAll("_"); id= "_"+id.toLowerCase(); while(id.length()>1 && id.endsWith("_")){ id=id.substring(0,id.length()-1); } while(id.indexOf("__")!=-1){ id=id.replaceAll("__", "_"); } return id; } AsciiDoctorHeadline(int deep, String name, int position, int end, int lengthTonNameEnd); String getCalculatedId(); int getLengthToNameEnd(); @Override int getSelectionStart(); @Override int getSelectionLength(); int getDeep(); String getName(); @Override int getPosition(); int getEnd(); String getId(); @Override String toString();  }
@Test public void calculateId_x_space_y_is__converted_to_underscore_x_underscore_y() { assertEquals("_x_y",AsciiDoctorHeadline.calculateId("x y")); }
@Test public void calculateId_X_space_y_is__converted_to_underscore_x_underscore_y() { assertEquals("_x_y",AsciiDoctorHeadline.calculateId("X y")); }
@Test public void calculateId_more_complex_scenarios() { assertEquals("_alpha_centauri_is_coool",AsciiDoctorHeadline.calculateId("Alpha centauri is!!!!coool!!!")); assertEquals("_alpha_centauri_is_coool",AsciiDoctorHeadline.calculateId("Alpha centauri is!!-----!coool!!!")); assertEquals("_alpha_centauri_is_coool",AsciiDoctorHeadline.calculateId("Alpha centauri is-coool!!!")); assertEquals("_alpha_centauri_is_coool",AsciiDoctorHeadline.calculateId("Alpha centauri is coool...")); }