Capitalise.py (bd8ef897) | Capitalise.py (28e51104) |
---|---|
1# ************************************************************* 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance --- 12 unchanged lines hidden (view full) --- 21 22 23# helper function 24def getNewString( theString ) : 25 if( not theString or len(theString) ==0) : 26 return "" 27 # should we tokenize on "."? 28 if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() : | 1# ************************************************************* 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance --- 12 unchanged lines hidden (view full) --- 21 22 23# helper function 24def getNewString( theString ) : 25 if( not theString or len(theString) ==0) : 26 return "" 27 # should we tokenize on "."? 28 if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() : |
29 # first two chars are UC => first UC, rest LC | 29 # first two chars are UC => first UC, rest LC |
30 newString=theString[0:1].upper() + theString[1:].lower(); 31 elif theString[0].isupper(): | 30 newString=theString[0:1].upper() + theString[1:].lower(); 31 elif theString[0].isupper(): |
32 # first char UC => all to LC | 32 # first char UC => all to LC |
33 newString=theString.lower() 34 else: # all to UC. 35 newString=theString.upper() 36 return newString; 37 | 33 newString=theString.lower() 34 else: # all to UC. 35 newString=theString.upper() 36 return newString; 37 |
38def capitalisePython( ): | 38def capitalisePython( ): |
39 """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case...""" 40 import string 41 42 # The context variable is of type XScriptContext and is available to 43 # all BeanShell scripts executed by the Script Framework 44 xModel = XSCRIPTCONTEXT.getDocument() 45 46 #the writer controller impl supports the css.view.XSelectionSupplier interface 47 xSelectionSupplier = xModel.getCurrentController() 48 49 #see section 7.5.1 of developers' guide 50 xIndexAccess = xSelectionSupplier.getSelection() 51 count = xIndexAccess.getCount(); 52 if(count>=1): #ie we have a selection 53 i=0 | 39 """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case...""" 40 import string 41 42 # The context variable is of type XScriptContext and is available to 43 # all BeanShell scripts executed by the Script Framework 44 xModel = XSCRIPTCONTEXT.getDocument() 45 46 #the writer controller impl supports the css.view.XSelectionSupplier interface 47 xSelectionSupplier = xModel.getCurrentController() 48 49 #see section 7.5.1 of developers' guide 50 xIndexAccess = xSelectionSupplier.getSelection() 51 count = xIndexAccess.getCount(); 52 if(count>=1): #ie we have a selection 53 i=0 |
54 while i < count : | 54 while i < count : |
55 xTextRange = xIndexAccess.getByIndex(i); 56 #print "string: " + xTextRange.getString(); 57 theString = xTextRange.getString(); 58 if len(theString)==0 : 59 # sadly we can have a selection where nothing is selected 60 # in this case we get the XWordCursor and make a selection! 61 xText = xTextRange.getText(); 62 xWordCursor = xText.createTextCursorByRange(xTextRange); --- 6 unchanged lines hidden (view full) --- 69 xWordCursor.setString(newString); 70 xSelectionSupplier.select(xWordCursor); 71 else : 72 73 newString = getNewString( theString ); 74 if newString: 75 xTextRange.setString(newString); 76 xSelectionSupplier.select(xTextRange); | 55 xTextRange = xIndexAccess.getByIndex(i); 56 #print "string: " + xTextRange.getString(); 57 theString = xTextRange.getString(); 58 if len(theString)==0 : 59 # sadly we can have a selection where nothing is selected 60 # in this case we get the XWordCursor and make a selection! 61 xText = xTextRange.getText(); 62 xWordCursor = xText.createTextCursorByRange(xTextRange); --- 6 unchanged lines hidden (view full) --- 69 xWordCursor.setString(newString); 70 xSelectionSupplier.select(xWordCursor); 71 else : 72 73 newString = getNewString( theString ); 74 if newString: 75 xTextRange.setString(newString); 76 xSelectionSupplier.select(xTextRange); |
77 i+= 1 | 77 i+= 1 |
78 79 80# lists the scripts, that shall be visible inside OOo. Can be omited, if 81# all functions shall be visible, however here getNewString shall be surpressed 82g_exportedScripts = capitalisePython, | 78 79 80# lists the scripts, that shall be visible inside OOo. Can be omited, if 81# all functions shall be visible, however here getNewString shall be surpressed 82g_exportedScripts = capitalisePython, |